45332023-03-29 12:51:37TomaSajtSípálya (55 pont)cpp17Accepted 55/5529ms13412 KiB
#include <bits/stdc++.h>
using namespace std;
#define speed cin.tie(0); ios::sync_with_stdio(0)
typedef long long ll;

vector<ll> range_maxes(const vector<ll>& data, int k) {
    int n = data.size();
    vector<ll> res(n - k + 1);
    deque<int> dq;
    for (int i = 0; i < n; i++) {
        while (!dq.empty() && i - dq.front() >= k) dq.pop_front();
        while (!dq.empty() && data[dq.back()] <= data[i]) dq.pop_back();
        dq.push_back(i);
        if (i >= k - 1) {
            res[i - k + 1] = data[dq.front()];
        }
    }
    return res;
}

vector<ll> range_sums(const vector<ll>& data, int k) {
    int n = data.size();
    vector<ll> res(n - k + 1);
    ll sum = 0;
    for (int i = 0; i < n; i++) {
        sum += data[i];
        if (i >= k) {
            sum -= data[i - k];
        }
        if (i >= k - 1) {
            res[i - k + 1] = sum;
        }
    }
    return res;
}

int main() {
    speed;
    int n, k; cin >> n >> k;
    vector<ll> data(n);
    for (auto& a : data) cin >> a;
    for (int i = 0; i < n; i++) data[i] += i;
    vector<ll> maxes = range_maxes(data, k);
    vector<ll> sums = range_sums(data, k);
    ll best = LLONG_MAX;
    for (int i = 0; i <= n - k; i++) {
        ll cost = k * maxes[i] - sums[i];
        best = min(best, cost);
    }
    cout << best;
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base55/55
1Accepted0/03ms1828 KiB
2Accepted0/03ms2196 KiB
3Accepted2/23ms2480 KiB
4Accepted2/23ms2640 KiB
5Accepted2/23ms2928 KiB
6Accepted2/23ms3148 KiB
7Accepted3/33ms3360 KiB
8Accepted1/14ms4024 KiB
9Accepted1/14ms3860 KiB
10Accepted1/14ms3676 KiB
11Accepted1/14ms3932 KiB
12Accepted1/14ms3940 KiB
13Accepted1/14ms4160 KiB
14Accepted2/24ms4360 KiB
15Accepted2/24ms4588 KiB
16Accepted2/228ms13236 KiB
17Accepted2/227ms10344 KiB
18Accepted2/227ms10396 KiB
19Accepted3/326ms8872 KiB
20Accepted2/228ms12980 KiB
21Accepted2/227ms13236 KiB
22Accepted2/228ms13216 KiB
23Accepted2/228ms13332 KiB
24Accepted2/229ms13324 KiB
25Accepted2/228ms13412 KiB
26Accepted2/228ms13380 KiB
27Accepted2/228ms12888 KiB
28Accepted3/327ms12888 KiB
29Accepted3/327ms12760 KiB
30Accepted3/327ms12760 KiB