81672024-01-12 15:30:22bovizdbSípálya (55 pont)cpp17Wrong answer 33/5530ms6696 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];
    }
    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)
        {
            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
base33/55
1Accepted0/03ms1828 KiB
2Wrong answer0/03ms2204 KiB
3Wrong answer0/23ms2328 KiB
4Wrong answer0/23ms2820 KiB
5Wrong answer0/23ms2928 KiB
6Wrong answer0/23ms3004 KiB
7Accepted3/33ms2984 KiB
8Accepted1/14ms3128 KiB
9Accepted1/14ms3020 KiB
10Accepted1/14ms3280 KiB
11Wrong answer0/14ms3232 KiB
12Wrong answer0/14ms3512 KiB
13Accepted1/14ms3508 KiB
14Wrong answer0/24ms3524 KiB
15Wrong answer0/24ms3520 KiB
16Wrong answer0/228ms6340 KiB
17Accepted2/228ms6400 KiB
18Accepted2/228ms6340 KiB
19Accepted3/328ms6344 KiB
20Accepted2/229ms6388 KiB
21Wrong answer0/228ms6392 KiB
22Accepted2/228ms6448 KiB
23Accepted2/228ms6440 KiB
24Wrong answer0/230ms6392 KiB
25Accepted2/228ms6392 KiB
26Accepted2/229ms6672 KiB
27Wrong answer0/228ms6696 KiB
28Accepted3/328ms6552 KiB
29Accepted3/328ms6692 KiB
30Accepted3/328ms6544 KiB