82142024-01-12 20:18:38gergomiszoriSípálya (55 pont)cpp17Wrong answer 6/55500ms6748 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

const ll MOD = 1e9+7;

void solve()
{
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    
    for(ll i = 0; i < n; i++) cin >> a[i];
    
    if(k == 1)
    {
        cout << 0 << endl;
        return;
    }

    ll ans = LLONG_MAX-1;

    ll maxe = -1;
    ll maxl = 0;

    ll ans2 = 0;

    for(ll i = 0; i < n-k+1; i++)
    {
        if(i == 0)
        {
            for(ll j = 0; j < k; j++)
            {
                if(a[i+j] >= maxe)
                {
                    maxe = a[i+j];
                    maxl = j;
                }
            }
            for(ll j = 0; j < k; j++)
            {
                ll magas = maxl-j + maxe;
                ans2 += magas - a[i+j];
            }
        }
        else
        {
            ans2 -= (maxl + maxe) - a[i-1];
            //cout << ans2 << " ";
            ll fsize = (maxl + maxe) -1;
            maxl--;
            if(maxl < 0 || a[i+k-1] >= maxe)
            {
                for(ll j = 0; j < k; j++)
                {
                    if(a[i+j] >= maxe)
                    {
                        maxe = a[i+j];
                        maxl = j;
                    }
                }
                ans2 += (maxe - (fsize - maxl)) * (k-1);
                //cout << ans2 << " ";
            }
            ans2 += (maxl-(k-1)) + maxe - a[i+k-1];
            //cout << ans2 << endl;
        }
        ans = min(ans, ans2);
    }
    cout << ans << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base6/55
1Accepted0/03ms1828 KiB
2Wrong answer0/03ms2052 KiB
3Wrong answer0/23ms2276 KiB
4Wrong answer0/23ms2328 KiB
5Wrong answer0/23ms2544 KiB
6Wrong answer0/23ms2764 KiB
7Accepted3/33ms2740 KiB
8Wrong answer0/14ms3084 KiB
9Wrong answer0/14ms3344 KiB
10Wrong answer0/14ms3304 KiB
11Wrong answer0/117ms3524 KiB
12Wrong answer0/117ms3540 KiB
13Accepted1/132ms3792 KiB
14Wrong answer0/221ms3920 KiB
15Wrong answer0/216ms3880 KiB
16Time limit exceeded0/2500ms4700 KiB
17Accepted2/228ms6748 KiB
18Time limit exceeded0/2453ms4800 KiB
19Time limit exceeded0/3474ms4640 KiB
20Time limit exceeded0/2474ms4632 KiB
21Time limit exceeded0/2455ms4732 KiB
22Time limit exceeded0/2451ms4648 KiB
23Time limit exceeded0/2462ms4716 KiB
24Time limit exceeded0/2469ms4916 KiB
25Time limit exceeded0/2470ms4832 KiB
26Time limit exceeded0/2458ms5036 KiB
27Time limit exceeded0/2433ms4860 KiB
28Time limit exceeded0/3477ms5056 KiB
29Time limit exceeded0/3474ms4832 KiB
30Time limit exceeded0/3462ms4848 KiB