57142023-09-09 19:43:08kukkermanSípálya (55 pont)cpp14Accepted 55/5590ms8184 KiB
#include <iostream>
#include <vector>
#include <stack>

class MaxVerem {
public:
    MaxVerem() = default;

    bool empty() const {
        return s_.empty();
    }

    size_t size() const {
        return s_.size();
    }

    int top() const {
        return s_.top().first;
    }

    int max() const {
        return s_.top().second;
    }

    void pop() {
        s_.pop();
    }

    void push(int e) {
        if (empty()) {
            s_.emplace(e, e);

        } else {
            s_.emplace(e, std::max(e, max()));
        }
    }

private:
    std::stack<std::pair<int, int>> s_;
};

class MaxSor {
public:
    MaxSor() = default;

    bool empty() const {
        return in_.empty() && out_.empty();
    }

    size_t size() const {
        return in_.size() + out_.size();
    }

    int top() {
        fill_out();
        return out_.top();
    }

    int max() const {
        const auto in_empty = in_.empty();
        const auto out_empty = out_.empty();

        if (in_empty && out_empty) {
            return 0;

        } else if (!in_empty && !out_empty) {
            return std::max(in_.max(), out_.max());

        } else if (in_empty) {
            return out_.max();

        } else {
            return in_.max();
        }
    }

    void pop() {
        fill_out();
        out_.pop();
    }

    void push(int e) {
        in_.push(e);
    }

private:
    void fill_out() {
        if (out_.empty()) {
            while (!in_.empty()) {
                out_.push(in_.top());
                in_.pop();
            }
        }
    }

    MaxVerem in_, out_;
};

void beolvas(std::istream &in, std::vector<int> &h, int &k) {
    size_t n;
    in >> n >> k;

    h.resize(n);
    for (auto i = 0u; i < n; i++) {
        in >> h[i];
    }
}

void feldolgoz(const std::vector<int> &h, int k) {
    MaxSor s;

    uint64_t koltseg = 0;
    s.push(h[0]);
    for (auto i = 1; i < k; i++) {
        const auto max_mag = s.max();
        const auto uj_mag = h[i] + i;

        if (uj_mag <= max_mag) {
            koltseg += max_mag - uj_mag;

        } else {
            koltseg += static_cast<uint64_t>(uj_mag - max_mag) * static_cast<uint64_t>(s.size());
        }

        s.push(uj_mag);
    }

    auto min_koltseg = koltseg;
    const auto n = static_cast<int>(h.size());
    for (auto i = k; i < n; i++) {
        const auto regi_max_mag = s.max();
        koltseg -= regi_max_mag - s.top();
        s.pop();

        const auto max_mag = s.max();
        koltseg -= static_cast<uint64_t>(regi_max_mag - max_mag) * static_cast<uint64_t>(s.size());

        const auto uj_mag = h[i] + i;
        if (uj_mag <= max_mag) {
            koltseg += max_mag - uj_mag;

        } else {
            koltseg += static_cast<uint64_t>(uj_mag - max_mag) * static_cast<uint64_t>(s.size());
        }
        min_koltseg = std::min(min_koltseg, koltseg);

        s.push(uj_mag);
    }

    std::cout << min_koltseg << std::endl;
}

int main() {
    std::vector<int> h;
    int k;
    beolvas(std::cin, h, k);
    feldolgoz(h, k);

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base55/55
1Accepted0/03ms1960 KiB
2Accepted0/03ms1924 KiB
3Accepted2/23ms2012 KiB
4Accepted2/23ms2136 KiB
5Accepted2/23ms2352 KiB
6Accepted2/23ms2432 KiB
7Accepted3/33ms2572 KiB
8Accepted1/16ms2688 KiB
9Accepted1/16ms3016 KiB
10Accepted1/16ms3228 KiB
11Accepted1/16ms3560 KiB
12Accepted1/16ms3720 KiB
13Accepted1/16ms3700 KiB
14Accepted2/27ms4028 KiB
15Accepted2/26ms3908 KiB
16Accepted2/282ms5192 KiB
17Accepted2/281ms7248 KiB
18Accepted2/279ms7368 KiB
19Accepted3/386ms8184 KiB
20Accepted2/283ms6304 KiB
21Accepted2/282ms6132 KiB
22Accepted2/283ms6296 KiB
23Accepted2/282ms6096 KiB
24Accepted2/290ms6424 KiB
25Accepted2/282ms6464 KiB
26Accepted2/282ms6416 KiB
27Accepted2/282ms6548 KiB
28Accepted3/381ms6544 KiB
29Accepted3/382ms6544 KiB
30Accepted3/382ms6668 KiB