81882024-01-12 16:48:08szabelrSípálya (55 pont)cpp17Accepted 55/55319ms8276 KiB
#include <iostream>

using namespace std;

long epit(long h[], long s, long K, long N) {
    long maxi = 0;
    for (long i = 0; i < K; i++)
        if (h[s + i] + i > h[s + maxi] + maxi) maxi = i;
    /* cout << "maxi = " << maxi << endl; */

    long m = h[s + maxi] - (K - 1 - maxi); // utolso lepcsofok magassaga
    /* cout << "m = " << m << endl; */

    long hs = 0;
    for (long i = 0; i < K; i++) hs += h[s + i];

    return K * (K - 1) / 2 + K * m - hs;
}

int main() {

    long N, K, h[200000];

    cin >> N >> K;
    for (long i = 0; i < N; i++) cin >> h[i];

    long maxi = 0;
    for (long i = 0; i < K; i++)
        if (h[i] + i > h[maxi] + maxi) maxi = i;
    long m = h[maxi] - (K - 1 - maxi); // utolso lepcsofok magassaga
    long hs = 0;
    for (long i = 0; i < K; i++) hs += h[i];

    long minp = K * (K - 1) / 2 + K * m - hs;

    for (long s = 1; s < N - K + 1; s++) {
        /* long p = epit(h, s, K, N); */

        if (maxi > 0) {
            if (h[s - 1 + maxi] + maxi - 1 > h[s + K - 1] + K - 1) maxi--;
            else maxi = K - 1;
        }
        else {
            maxi = 0;
            for (long i = 0; i < K; i++)
                if (h[s + i] + i > h[s + maxi] + maxi) maxi = i;
        }

        m = h[s + maxi] - (K - 1 - maxi); // utolso lepcsofok magassaga
        hs = hs - h[s - 1] + h[s + K - 1];
        long p = K * (K - 1) / 2 + K * m - hs;
        if (p < minp) minp = p;
    }
    cout << minp;
}
SubtaskSumTestVerdictTimeMemory
base55/55
1Accepted0/04ms5116 KiB
2Accepted0/04ms5484 KiB
3Accepted2/24ms5612 KiB
4Accepted2/24ms5708 KiB
5Accepted2/24ms5880 KiB
6Accepted2/24ms5788 KiB
7Accepted3/34ms5792 KiB
8Accepted1/17ms5916 KiB
9Accepted1/17ms6132 KiB
10Accepted1/17ms6364 KiB
11Accepted1/114ms6288 KiB
12Accepted1/114ms6416 KiB
13Accepted1/121ms6560 KiB
14Accepted2/218ms6656 KiB
15Accepted2/213ms6628 KiB
16Accepted2/2219ms6628 KiB
17Accepted2/271ms6884 KiB
18Accepted2/271ms6972 KiB
19Accepted3/371ms6984 KiB
20Accepted2/2109ms7056 KiB
21Accepted2/2127ms7152 KiB
22Accepted2/2210ms7268 KiB
23Accepted2/2238ms7472 KiB
24Accepted2/2319ms7548 KiB
25Accepted2/2118ms7736 KiB
26Accepted2/2130ms7808 KiB
27Accepted2/271ms8028 KiB
28Accepted3/379ms8120 KiB
29Accepted3/371ms8040 KiB
30Accepted3/371ms8276 KiB