81742024-01-12 15:43:29bovizdbSípálya (55 pont)cpp17Runtime error 0/55430ms6624 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
#define ll long long
#define endl "\n"
#define pll pair<ll, ll>
#define fs first
#define sc second
#define vll vector<ll>
#define v2d vector<vector<ll>>
#define vpll vector<pll>
#define qll queue<ll>
#define stll stack<ll>
#define sll set<ll>
#define mll map<ll, ll>

int solve()
{
    ll n, k;
    cin >> n >> k;
    vll h(n);
    for (ll i = 0; i < n; i++)
    {
        cin >> h[i];
    }
    if (k == 1)
    {
        cout << 0;
        return 0;
    }
    ll mx = 0, mxpos = 0;
    for (ll i = 0; i < k; i++)
    {
        if (h[i] > mx+(mxpos-i))
        {
            mx = h[i];
            mxpos = i;
        }
    }
    ll cost = 0;
    for (ll i = 0; i < k; i++)
    {
        cost += mx+(mxpos-i)-h[i];
    }
    ll out = cost;
    for (ll i = 1; i < n-k+1; i++)
    {
        cost -= mx+(mxpos-(i-1))-h[i-1];
        ll oldmx = mx, oldpos = mxpos;
        if (h[i+k-1] > mx+(mxpos-(i+k-1)))
        {
            cost += (k-1)*(h[i+k-1] - (mx+(mxpos-(i+k-1))));
            mx = h[i+k-1];
            mxpos = i+k-1;
        }
        else if (mxpos == i-1)
        {
            mx = 0;
            mxpos = 0;
            for (ll j = 0; j < k-1; j++)
            {
                if (h[i+j] > mx+(mxpos-(i+j)))
                {
                    mx = h[i+j];
                    mxpos = i+j;
                }
            }
            cost -= (k-1)*((oldmx+(oldpos-i)-h[i]) - (mx+(mxpos-i)-h[i]));
            cost += mx+(mxpos-(i+k-1))-h[i+k-1];
        }
        else
        {
            cost += mx+(mxpos-(i+k-1))-h[i+k-1];
        }
        out = min(out, cost);
    }
    cout  << out;
}
 
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base0/55
1Runtime error0/03ms2216 KiB
2Runtime error0/03ms2280 KiB
3Runtime error0/23ms2600 KiB
4Runtime error0/23ms2408 KiB
5Runtime error0/23ms2588 KiB
6Runtime error0/23ms2796 KiB
7Runtime error0/33ms3080 KiB
8Runtime error0/14ms3412 KiB
9Runtime error0/14ms3380 KiB
10Runtime error0/14ms3412 KiB
11Runtime error0/116ms3336 KiB
12Runtime error0/114ms3336 KiB
13Runtime error0/123ms3488 KiB
14Runtime error0/218ms3640 KiB
15Runtime error0/213ms3584 KiB
16Runtime error0/2349ms6176 KiB
17Runtime error0/228ms6352 KiB
18Runtime error0/228ms6616 KiB
19Runtime error0/328ms6476 KiB
20Runtime error0/282ms6552 KiB
21Runtime error0/2317ms6476 KiB
22Runtime error0/2372ms6476 KiB
23Runtime error0/2270ms6556 KiB
24Time limit exceeded0/2430ms4516 KiB
25Runtime error0/297ms6476 KiB
26Runtime error0/2111ms6552 KiB
27Runtime error0/2105ms6476 KiB
28Runtime error0/359ms6624 KiB
29Runtime error0/397ms6476 KiB
30Runtime error0/329ms6480 KiB