123832024-12-15 23:44:42kukkermanSípálya (55 pont)cpp11Accepted 55/5530ms1592 KiB
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

int main() {
    FILE *be = stdin;

    uint64_t n, k;
    fscanf(be, "%llu %llu", &n, &k);

    uint64_t *sor = (uint64_t*)malloc((k + 1) * sizeof(uint64_t));
    uint64_t *max_sor = (uint64_t*)malloc((k + 1) * sizeof(uint64_t));

    fscanf(be, "%llu", sor);
    uint64_t sor_eleje = 0, sor_vege = 0;

    max_sor[0] = sor[0];
    uint64_t max_sor_eleje = 0, max_sor_vege = 0, max_sor_db = 1;

    uint64_t akt_koltseg = 0;
    uint64_t i;
    for (i = 1; i < k; i++) {
        ++sor_vege;
        fscanf(be, "%llu", sor + sor_vege);
        sor[sor_vege] += i;
        const uint64_t akt_elem = sor[sor_vege];

        const uint64_t elozo_max = max_sor[max_sor_eleje];
        while (max_sor_db > 0 && max_sor[max_sor_vege] < akt_elem) {
            --max_sor_vege;
            --max_sor_db;
        }
        max_sor[++max_sor_vege] = akt_elem;
        ++max_sor_db;
        const uint64_t akt_max = max_sor[max_sor_eleje];

        akt_koltseg += elozo_max < akt_max ? (akt_max - elozo_max) * i : akt_max - akt_elem;
    }

    uint64_t min_koltseg = akt_koltseg;
    for (; i < n; i++) {
        if (++sor_vege > k) {
            sor_vege = 0;
        }
        fscanf(be, "%llu", sor + sor_vege);
        sor[sor_vege] += i;
        const uint64_t akt_elem = sor[sor_vege];

        uint64_t elozo_max = max_sor[max_sor_eleje];
        while (max_sor_db > 0 && max_sor[max_sor_vege] < akt_elem) {
            max_sor_vege = max_sor_vege > 0 ? max_sor_vege - 1 : k;
            --max_sor_db;
        }
        if (++max_sor_vege > k) {
            max_sor_vege = 0;
        }
        max_sor[max_sor_vege] = akt_elem;
        ++max_sor_db;
        uint64_t akt_max = max_sor[max_sor_eleje];

        akt_koltseg += elozo_max < akt_max ? (akt_max - elozo_max) * k : akt_max - akt_elem;

        if (max_sor[max_sor_eleje] == sor[sor_eleje]) {
            elozo_max = akt_max;
            if (++max_sor_eleje > k) {
                max_sor_eleje = 0;
            }
            --max_sor_db;
            akt_max = max_sor[max_sor_eleje];

            akt_koltseg -= (elozo_max - akt_max) * k;

        } else {
            akt_koltseg -= akt_max - sor[sor_eleje];
        }

        if (++sor_eleje > k) {
            sor_eleje = 0;
        }

        min_koltseg = akt_koltseg < min_koltseg ? akt_koltseg : min_koltseg;
    }

    printf("%llu\n", min_koltseg);

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base55/55
1Accepted0/01ms320 KiB
2Accepted0/01ms508 KiB
3Accepted2/21ms320 KiB
4Accepted2/21ms320 KiB
5Accepted2/21ms320 KiB
6Accepted2/21ms320 KiB
7Accepted3/31ms320 KiB
8Accepted1/12ms320 KiB
9Accepted1/12ms320 KiB
10Accepted1/12ms320 KiB
11Accepted1/12ms320 KiB
12Accepted1/12ms580 KiB
13Accepted1/12ms320 KiB
14Accepted2/23ms320 KiB
15Accepted2/22ms320 KiB
16Accepted2/229ms320 KiB
17Accepted2/229ms1152 KiB
18Accepted2/229ms1244 KiB
19Accepted3/329ms1592 KiB
20Accepted2/229ms568 KiB
21Accepted2/229ms620 KiB
22Accepted2/229ms568 KiB
23Accepted2/229ms540 KiB
24Accepted2/230ms576 KiB
25Accepted2/229ms568 KiB
26Accepted2/229ms568 KiB
27Accepted2/229ms720 KiB
28Accepted3/329ms568 KiB
29Accepted3/329ms736 KiB
30Accepted3/329ms804 KiB