123822024-12-15 22:05:27kukkermanSípálya (55 pont)cpp17Accepted 55/5527ms1484 KiB
#include <iostream>
#include <cstdint>
#include <deque>
#include <algorithm>

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    
    auto &be = std::cin;

    uint64_t n, k;
    be >> n >> k;

    std::deque<uint64_t> sor, max_sor;

    uint64_t m;
    be >> m;
    sor.push_back(m);
    max_sor.push_back(m);

    uint64_t i, akt_koltseg = 0;
    for (i = 1; i < k; i++) {
        be >> m;
        m += i;
        sor.push_back(m);

        const auto elozo_max_magassag = max_sor.front();
        while (!max_sor.empty() && max_sor.back() < m) {
            max_sor.pop_back();
        }
        max_sor.push_back(m);
        const auto akt_max_magassag = max_sor.front();

        akt_koltseg += (elozo_max_magassag < akt_max_magassag)     ?
                       (akt_max_magassag - elozo_max_magassag) * i :
                       (akt_max_magassag - m);
    }

    auto min_koltseg = akt_koltseg;
    for (; i < n; i++) {
        be >> m;
        m += i;
        sor.push_back(m);

        auto elozo_max_magassag = max_sor.front();
        while (!max_sor.empty() && max_sor.back() < m) {
            max_sor.pop_back();
        }
        max_sor.push_back(m);
        auto akt_max_magassag = max_sor.front();

        akt_koltseg += (elozo_max_magassag < akt_max_magassag)     ?
                       (akt_max_magassag - elozo_max_magassag) * k :
                       (akt_max_magassag - m);

        const auto bal = sor.front();
        sor.pop_front();
        if (akt_max_magassag == bal) {
            max_sor.pop_front();
            elozo_max_magassag = akt_max_magassag;
            akt_max_magassag = max_sor.front();

            akt_koltseg -= (elozo_max_magassag - akt_max_magassag) * k;

        } else {
            akt_koltseg -= akt_max_magassag - bal;
        }

        min_koltseg = std::min(min_koltseg, akt_koltseg);
    }

    std::cout << min_koltseg << '\n';

    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/12ms500 KiB
12Accepted1/12ms320 KiB
13Accepted1/12ms504 KiB
14Accepted2/22ms320 KiB
15Accepted2/22ms636 KiB
16Accepted2/226ms532 KiB
17Accepted2/226ms1228 KiB
18Accepted2/226ms1448 KiB
19Accepted3/326ms1484 KiB
20Accepted2/226ms500 KiB
21Accepted2/225ms568 KiB
22Accepted2/226ms660 KiB
23Accepted2/226ms576 KiB
24Accepted2/227ms568 KiB
25Accepted2/226ms680 KiB
26Accepted2/226ms568 KiB
27Accepted2/226ms772 KiB
28Accepted3/325ms568 KiB
29Accepted3/326ms780 KiB
30Accepted3/326ms824 KiB