45332023-03-29 12:51:37TomaSajtSípálya (55 pont)cpp17Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base55/55
1Elfogadva0/03ms1828 KiB
2Elfogadva0/03ms2196 KiB
3Elfogadva2/23ms2480 KiB
4Elfogadva2/23ms2640 KiB
5Elfogadva2/23ms2928 KiB
6Elfogadva2/23ms3148 KiB
7Elfogadva3/33ms3360 KiB
8Elfogadva1/14ms4024 KiB
9Elfogadva1/14ms3860 KiB
10Elfogadva1/14ms3676 KiB
11Elfogadva1/14ms3932 KiB
12Elfogadva1/14ms3940 KiB
13Elfogadva1/14ms4160 KiB
14Elfogadva2/24ms4360 KiB
15Elfogadva2/24ms4588 KiB
16Elfogadva2/228ms13236 KiB
17Elfogadva2/227ms10344 KiB
18Elfogadva2/227ms10396 KiB
19Elfogadva3/326ms8872 KiB
20Elfogadva2/228ms12980 KiB
21Elfogadva2/227ms13236 KiB
22Elfogadva2/228ms13216 KiB
23Elfogadva2/228ms13332 KiB
24Elfogadva2/229ms13324 KiB
25Elfogadva2/228ms13412 KiB
26Elfogadva2/228ms13380 KiB
27Elfogadva2/228ms12888 KiB
28Elfogadva3/327ms12888 KiB
29Elfogadva3/327ms12760 KiB
30Elfogadva3/327ms12760 KiB