74972024-01-09 11:03:44AblablablaSípálya (55 pont)cpp14Accepted 55/5598ms21768 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair<ll, ll> pii;

const ll INF = 4e18 + 7;

ll ind = 0;

struct comp{
    bool operator()(pii a, pii b){
        ll c = a.first + a.second - ind;
        ll d = b.first + b.second - ind;

        return c < d;
    }
};

int main()
{
    ll n, k;
    cin >> n >> k;

    vector<ll> hegyek(n, 0);

    for(ll i = 0; i < n; i++){
        cin >> hegyek[i];
    }

    vector<ll> pref(n + 1, 0);
    for(ll i = 1; i <= n; i++){
        pref[i] = pref[i - 1] + hegyek[i - 1];
    }

    vector<ll> kezdo(n, 0);
    priority_queue<pii, vector<pii>, comp> sorrend;

    for(ll i = 0; i < k; i++){
        sorrend.push({hegyek[i], i});
    }

    for(ll i = 0; i + k - 1 < n; i++){
        ind = i;
        sorrend.push({hegyek[i + k - 1], i + k - 1});
        while(sorrend.top().second < i){
            sorrend.pop();
        }

        ll kesobb = sorrend.top().second - i;
        kezdo[i] = sorrend.top().first + kesobb;
    }

    ll mini = INF;
    for(ll i = 0; i + k - 1 < n; i++){
        ll koltseg = (kezdo[i] + kezdo[i] - k + 1) * k / 2 - (pref[i + k] - pref[i]);
        mini = min(mini, koltseg);
    }
    cout << mini << "\n";
}
SubtaskSumTestVerdictTimeMemory
base55/55
1Accepted0/03ms1808 KiB
2Accepted0/03ms2152 KiB
3Accepted2/23ms2320 KiB
4Accepted2/23ms2676 KiB
5Accepted2/23ms2788 KiB
6Accepted2/23ms2876 KiB
7Accepted3/33ms2808 KiB
8Accepted1/17ms3856 KiB
9Accepted1/17ms4080 KiB
10Accepted1/17ms4036 KiB
11Accepted1/17ms3668 KiB
12Accepted1/17ms3928 KiB
13Accepted1/17ms3976 KiB
14Accepted2/27ms3932 KiB
15Accepted2/27ms4192 KiB
16Accepted2/292ms17168 KiB
17Accepted2/286ms21276 KiB
18Accepted2/286ms21212 KiB
19Accepted3/386ms21260 KiB
20Accepted2/289ms21192 KiB
21Accepted2/290ms21212 KiB
22Accepted2/292ms21192 KiB
23Accepted2/290ms21192 KiB
24Accepted2/298ms21192 KiB
25Accepted2/289ms21388 KiB
26Accepted2/287ms21332 KiB
27Accepted2/286ms21568 KiB
28Accepted3/386ms21564 KiB
29Accepted3/386ms21584 KiB
30Accepted3/387ms21768 KiB