123812024-12-15 22:02:37kukkermanSípálya (55 pont)cpp17Accepted 55/5575ms1520 KiB
#include <iostream>
#include <cstdint>
#include <deque>
#include <algorithm>

int main() {
    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/22ms320 KiB
4Accepted2/21ms320 KiB
5Accepted2/22ms320 KiB
6Accepted2/21ms412 KiB
7Accepted3/31ms320 KiB
8Accepted1/14ms320 KiB
9Accepted1/14ms500 KiB
10Accepted1/14ms320 KiB
11Accepted1/14ms424 KiB
12Accepted1/14ms420 KiB
13Accepted1/14ms496 KiB
14Accepted2/24ms320 KiB
15Accepted2/24ms320 KiB
16Accepted2/268ms320 KiB
17Accepted2/272ms1272 KiB
18Accepted2/268ms1404 KiB
19Accepted3/371ms1520 KiB
20Accepted2/270ms612 KiB
21Accepted2/270ms588 KiB
22Accepted2/270ms636 KiB
23Accepted2/271ms548 KiB
24Accepted2/275ms500 KiB
25Accepted2/270ms620 KiB
26Accepted2/270ms672 KiB
27Accepted2/268ms568 KiB
28Accepted3/370ms632 KiB
29Accepted3/368ms844 KiB
30Accepted3/370ms608 KiB