81702024-01-12 15:38:10bovizdbSípálya (55 pont)cpp17Hibás válasz 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base51/55
1Elfogadva0/03ms1824 KiB
2Elfogadva0/03ms2056 KiB
3Hibás válasz0/23ms2432 KiB
4Elfogadva2/23ms2416 KiB
5Elfogadva2/23ms2500 KiB
6Elfogadva2/23ms2584 KiB
7Elfogadva3/33ms2708 KiB
8Elfogadva1/14ms3008 KiB
9Elfogadva1/14ms2960 KiB
10Elfogadva1/14ms2968 KiB
11Elfogadva1/114ms3240 KiB
12Elfogadva1/114ms3272 KiB
13Elfogadva1/123ms3152 KiB
14Elfogadva2/218ms3404 KiB
15Elfogadva2/213ms3756 KiB
16Elfogadva2/2347ms6584 KiB
17Elfogadva2/227ms6532 KiB
18Elfogadva2/227ms6528 KiB
19Elfogadva3/326ms6528 KiB
20Elfogadva2/279ms6532 KiB
21Elfogadva2/2314ms6528 KiB
22Elfogadva2/2370ms6588 KiB
23Elfogadva2/2266ms6676 KiB
24Időlimit túllépés0/2448ms4420 KiB
25Elfogadva2/296ms6528 KiB
26Elfogadva2/2108ms6528 KiB
27Elfogadva2/2103ms6660 KiB
28Elfogadva3/357ms6592 KiB
29Elfogadva3/396ms6912 KiB
30Elfogadva3/327ms7220 KiB