56992023-09-09 15:45:35kukkermanSípálya (55 pont)cpp14Wrong answer 18/5583ms7520 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;

    int 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 += (uj_mag - max_mag) * static_cast<int>(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 -= (regi_max_mag - max_mag) * (k - 1);

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

        } else {
            koltseg += (uj_mag - max_mag) * static_cast<int>(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
base18/55
1Accepted0/03ms1812 KiB
2Accepted0/03ms2056 KiB
3Accepted2/23ms2268 KiB
4Accepted2/23ms2484 KiB
5Accepted2/23ms2692 KiB
6Accepted2/23ms2904 KiB
7Wrong answer0/33ms3148 KiB
8Accepted1/16ms3240 KiB
9Accepted1/16ms3248 KiB
10Accepted1/16ms3244 KiB
11Accepted1/16ms3248 KiB
12Accepted1/16ms3448 KiB
13Accepted1/16ms3540 KiB
14Accepted2/26ms3544 KiB
15Accepted2/26ms3520 KiB
16Wrong answer0/278ms5084 KiB
17Wrong answer0/276ms6852 KiB
18Wrong answer0/275ms6960 KiB
19Wrong answer0/376ms7520 KiB
20Wrong answer0/278ms5392 KiB
21Wrong answer0/278ms5560 KiB
22Wrong answer0/279ms5792 KiB
23Wrong answer0/278ms5404 KiB
24Wrong answer0/283ms5940 KiB
25Wrong answer0/279ms5684 KiB
26Wrong answer0/278ms5684 KiB
27Wrong answer0/278ms5776 KiB
28Wrong answer0/378ms5772 KiB
29Wrong answer0/378ms6028 KiB
30Wrong answer0/378ms6116 KiB