81702024-01-12 15:38:10bovizdbSípálya (55 pont)cpp17Wrong answer 51/55448ms7220 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)
        {
            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/03ms1824 KiB
2Accepted0/03ms2056 KiB
3Wrong answer0/23ms2432 KiB
4Accepted2/23ms2416 KiB
5Accepted2/23ms2500 KiB
6Accepted2/23ms2584 KiB
7Accepted3/33ms2708 KiB
8Accepted1/14ms3008 KiB
9Accepted1/14ms2960 KiB
10Accepted1/14ms2968 KiB
11Accepted1/114ms3240 KiB
12Accepted1/114ms3272 KiB
13Accepted1/123ms3152 KiB
14Accepted2/218ms3404 KiB
15Accepted2/213ms3756 KiB
16Accepted2/2347ms6584 KiB
17Accepted2/227ms6532 KiB
18Accepted2/227ms6528 KiB
19Accepted3/326ms6528 KiB
20Accepted2/279ms6532 KiB
21Accepted2/2314ms6528 KiB
22Accepted2/2370ms6588 KiB
23Accepted2/2266ms6676 KiB
24Time limit exceeded0/2448ms4420 KiB
25Accepted2/296ms6528 KiB
26Accepted2/2108ms6528 KiB
27Accepted2/2103ms6660 KiB
28Accepted3/357ms6592 KiB
29Accepted3/396ms6912 KiB
30Accepted3/327ms7220 KiB