81752024-01-12 15:45:59bovizdbSípálya (55 pont)cpp17Hibás válasz 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base51/55
1Elfogadva0/03ms1832 KiB
2Elfogadva0/03ms2048 KiB
3Hibás válasz0/23ms2292 KiB
4Elfogadva2/23ms2328 KiB
5Elfogadva2/23ms2460 KiB
6Elfogadva2/23ms2672 KiB
7Elfogadva3/33ms2884 KiB
8Elfogadva1/14ms3036 KiB
9Elfogadva1/14ms3032 KiB
10Elfogadva1/14ms3156 KiB
11Elfogadva1/114ms3128 KiB
12Elfogadva1/114ms3140 KiB
13Elfogadva1/123ms3140 KiB
14Elfogadva2/218ms3164 KiB
15Elfogadva2/213ms3284 KiB
16Elfogadva2/2345ms5956 KiB
17Elfogadva2/227ms5960 KiB
18Elfogadva2/227ms6016 KiB
19Elfogadva3/327ms6012 KiB
20Elfogadva2/279ms6012 KiB
21Elfogadva2/2316ms6068 KiB
22Elfogadva2/2370ms5956 KiB
23Elfogadva2/2266ms6216 KiB
24Időlimit túllépés0/2433ms4056 KiB
25Elfogadva2/296ms6228 KiB
26Elfogadva2/2108ms6168 KiB
27Elfogadva2/2104ms6232 KiB
28Elfogadva3/357ms6168 KiB
29Elfogadva3/396ms6224 KiB
30Elfogadva3/327ms6276 KiB