81752024-01-12 15:45:59bovizdbSípálya (55 pont)cpp17Wrong answer 51/55433ms6276 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>

void 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;
    }
    else
    {
        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
base51/55
1Accepted0/03ms1832 KiB
2Accepted0/03ms2048 KiB
3Wrong answer0/23ms2292 KiB
4Accepted2/23ms2328 KiB
5Accepted2/23ms2460 KiB
6Accepted2/23ms2672 KiB
7Accepted3/33ms2884 KiB
8Accepted1/14ms3036 KiB
9Accepted1/14ms3032 KiB
10Accepted1/14ms3156 KiB
11Accepted1/114ms3128 KiB
12Accepted1/114ms3140 KiB
13Accepted1/123ms3140 KiB
14Accepted2/218ms3164 KiB
15Accepted2/213ms3284 KiB
16Accepted2/2345ms5956 KiB
17Accepted2/227ms5960 KiB
18Accepted2/227ms6016 KiB
19Accepted3/327ms6012 KiB
20Accepted2/279ms6012 KiB
21Accepted2/2316ms6068 KiB
22Accepted2/2370ms5956 KiB
23Accepted2/2266ms6216 KiB
24Time limit exceeded0/2433ms4056 KiB
25Accepted2/296ms6228 KiB
26Accepted2/2108ms6168 KiB
27Accepted2/2104ms6232 KiB
28Accepted3/357ms6168 KiB
29Accepted3/396ms6224 KiB
30Accepted3/327ms6276 KiB