103672024-04-01 13:24:36MagyarKendeSZLGSípálya (55 pont)cpp17Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base55/55
1Elfogadva0/03ms1828 KiB
2Elfogadva0/03ms2028 KiB
3Elfogadva2/23ms2400 KiB
4Elfogadva2/23ms2620 KiB
5Elfogadva2/23ms2844 KiB
6Elfogadva2/23ms3068 KiB
7Elfogadva3/33ms3032 KiB
8Elfogadva1/14ms3484 KiB
9Elfogadva1/14ms3816 KiB
10Elfogadva1/14ms3968 KiB
11Elfogadva1/14ms4088 KiB
12Elfogadva1/14ms4204 KiB
13Elfogadva1/14ms4320 KiB
14Elfogadva2/24ms4392 KiB
15Elfogadva2/24ms4460 KiB
16Elfogadva2/232ms14184 KiB
17Elfogadva2/230ms13692 KiB
18Elfogadva2/230ms14484 KiB
19Elfogadva3/330ms15000 KiB
20Elfogadva2/232ms18060 KiB
21Elfogadva2/232ms19148 KiB
22Elfogadva2/232ms20088 KiB
23Elfogadva2/232ms21068 KiB
24Elfogadva2/234ms22048 KiB
25Elfogadva2/232ms23060 KiB
26Elfogadva2/232ms23052 KiB
27Elfogadva2/232ms22928 KiB
28Elfogadva3/332ms22820 KiB
29Elfogadva3/330ms22784 KiB
30Elfogadva3/332ms22752 KiB