81942024-01-12 17:48:29MagyarKendeSZLGSípálya (55 pont)cpp17Wrong answer 53/5534ms13572 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
base53/55
1Accepted0/03ms1828 KiB
2Accepted0/03ms2020 KiB
3Accepted2/23ms2388 KiB
4Accepted2/23ms2472 KiB
5Accepted2/23ms2800 KiB
6Accepted2/23ms2760 KiB
7Accepted3/33ms2976 KiB
8Accepted1/14ms3376 KiB
9Accepted1/14ms3376 KiB
10Accepted1/14ms3244 KiB
11Accepted1/14ms3536 KiB
12Accepted1/14ms3608 KiB
13Accepted1/14ms3744 KiB
14Accepted2/24ms3832 KiB
15Accepted2/24ms3828 KiB
16Accepted2/232ms12600 KiB
17Wrong answer0/230ms11296 KiB
18Accepted2/230ms11464 KiB
19Accepted3/330ms11000 KiB
20Accepted2/232ms12964 KiB
21Accepted2/232ms13112 KiB
22Accepted2/232ms13224 KiB
23Accepted2/232ms13308 KiB
24Accepted2/234ms13572 KiB
25Accepted2/232ms13424 KiB
26Accepted2/232ms13120 KiB
27Accepted2/232ms12936 KiB
28Accepted3/332ms13084 KiB
29Accepted3/332ms12932 KiB
30Accepted3/332ms12932 KiB