103672024-04-01 13:24:36MagyarKendeSZLGSípálya (55 pont)cpp17Accepted 55/5534ms23060 KiB
#include <iostream>
#include <vector>
#include <deque>
#include <climits>
#include <numeric>
using namespace std;

using ll = unsigned long long;
#define speed cin.tie(0); ios::sync_with_stdio(0)

vector<ll> heightS;
int N, K;

int main() {
    speed;

    cin >> N >> K;

    heightS.resize(N);

    for (int i = 0; i < N; i++) {
        cin >> heightS[i];
        heightS[i] += i;
    }

    deque<pair<int, int>> q;
    vector<ll> window_mx(N - K + 1);
    
    for (int right = 0; right < K; right++) {
        while (!q.empty() && q.back().first < heightS[right]) q.pop_back();
        q.push_back({heightS[right], right});
    }

    window_mx[0] = q.front().first;
    int p = 0;

    int left = 1, right = K;
    while (left < N - K + 1) {
        while (!q.empty() && q.front().second < left) q.pop_front();

        while (!q.empty() && q.back().first < heightS[right]) q.pop_back();

        q.push_back({heightS[right], right});

        left++;
        right++;

        window_mx[++p] = q.front().first;
    }

    ll result = ULLONG_MAX;

    vector<ll> prefix(N);
    partial_sum(heightS.begin(), heightS.end(), prefix.begin() + 1);

    for (int i = 0; i <= N - K; i++) {
        result = min(
            result, 
            (window_mx[i] * K) - 
            (prefix[i + K] - prefix[i])
        );
    }

    cout << result;
}
SubtaskSumTestVerdictTimeMemory
base55/55
1Accepted0/03ms1828 KiB
2Accepted0/03ms2028 KiB
3Accepted2/23ms2400 KiB
4Accepted2/23ms2620 KiB
5Accepted2/23ms2844 KiB
6Accepted2/23ms3068 KiB
7Accepted3/33ms3032 KiB
8Accepted1/14ms3484 KiB
9Accepted1/14ms3816 KiB
10Accepted1/14ms3968 KiB
11Accepted1/14ms4088 KiB
12Accepted1/14ms4204 KiB
13Accepted1/14ms4320 KiB
14Accepted2/24ms4392 KiB
15Accepted2/24ms4460 KiB
16Accepted2/232ms14184 KiB
17Accepted2/230ms13692 KiB
18Accepted2/230ms14484 KiB
19Accepted3/330ms15000 KiB
20Accepted2/232ms18060 KiB
21Accepted2/232ms19148 KiB
22Accepted2/232ms20088 KiB
23Accepted2/232ms21068 KiB
24Accepted2/234ms22048 KiB
25Accepted2/232ms23060 KiB
26Accepted2/232ms23052 KiB
27Accepted2/232ms22928 KiB
28Accepted3/332ms22820 KiB
29Accepted3/330ms22784 KiB
30Accepted3/332ms22752 KiB