56882023-09-08 19:12:53kukkermanSípálya (55 pont)cpp14Wrong answer 4/5586ms27440 KiB
#include <iostream>
#include <vector>
#include <stack>

//#include <random>
//#include <chrono>

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++) {
        koltseg -= s.max() - s.top();
        s.pop();

        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());
        }
        min_koltseg = std::min(min_koltseg, koltseg);

        s.push(uj_mag);
    }

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

/*
void feldolgoz_negyzetes(const std::vector<int> &h, int k) {
    const auto n = static_cast<int>(h.size());

    auto min_koltseg = std::numeric_limits<int>::max();
    for (auto i = 0; i < n - k + 1; i++) {
        int m = 0;
        for (auto j = 0; j < k; j++) {
            m = std::max(m, h[i + j] + j);
        }

        int koltseg = 0;
        for (auto j = 0; j < k; j++) {
            koltseg += m - j - h[i + j];
        }

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

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

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

    /*
    static constexpr auto n = 200000;
    static constexpr auto k = n / 2;

    std::random_device rd;
    std::minstd_rand rand_engine(rd());
    //rand_engine.seed(123456);

    std::uniform_int_distribution<> rand_range(1, 10000);

    std::vector<int> v(n);
    for (auto i = 0; i < n; i++) {
        v[i] = rand_range(rand_engine);
    }

    auto start_time = std::chrono::steady_clock::now();
    feldolgoz(v, k);
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_time);
    std::cout << "min. queue: " << duration.count() << " ms\n\n";

    start_time = std::chrono::steady_clock::now();
    feldolgoz_negyzetes(v, k);
    duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_time);
    std::cout << "nested loops: " << duration.count() << " ms\n\n";
    */

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base4/55
1Accepted0/03ms1808 KiB
2Wrong answer0/03ms2056 KiB
3Wrong answer0/23ms2124 KiB
4Wrong answer0/23ms2336 KiB
5Wrong answer0/23ms2448 KiB
6Wrong answer0/23ms2696 KiB
7Wrong answer0/33ms2704 KiB
8Accepted1/16ms2772 KiB
9Accepted1/16ms3088 KiB
10Accepted1/16ms3420 KiB
11Wrong answer0/16ms3708 KiB
12Wrong answer0/16ms3992 KiB
13Accepted1/16ms4196 KiB
14Wrong answer0/26ms4284 KiB
15Wrong answer0/26ms4164 KiB
16Wrong answer0/276ms6936 KiB
17Wrong answer0/276ms10380 KiB
18Wrong answer0/275ms11960 KiB
19Wrong answer0/376ms13948 KiB
20Wrong answer0/278ms13220 KiB
21Wrong answer0/275ms14260 KiB
22Wrong answer0/276ms15728 KiB
23Wrong answer0/278ms16700 KiB
24Wrong answer0/285ms18532 KiB
25Wrong answer0/278ms20224 KiB
26Wrong answer0/278ms21492 KiB
27Wrong answer0/282ms23000 KiB
28Wrong answer0/376ms24372 KiB
29Wrong answer0/378ms25992 KiB
30Wrong answer0/386ms27440 KiB