結果

問題 No.2654 [Cherry 6th Tune] Re: start! (Black Sheep)
ユーザー 👑 hitonanodehitonanode
提出日時 2024-03-10 18:19:07
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,648 bytes
コンパイル時間 1,287 ms
コンパイル使用メモリ 104,016 KB
実行使用メモリ 57,592 KB
最終ジャッジ日時 2024-03-10 18:19:27
合計ジャッジ時間 19,405 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 233 ms
13,184 KB
testcase_04 AC 221 ms
12,612 KB
testcase_05 AC 121 ms
8,960 KB
testcase_06 AC 34 ms
6,548 KB
testcase_07 AC 98 ms
8,064 KB
testcase_08 AC 139 ms
9,600 KB
testcase_09 AC 235 ms
13,324 KB
testcase_10 AC 35 ms
6,676 KB
testcase_11 AC 157 ms
10,496 KB
testcase_12 AC 25 ms
6,676 KB
testcase_13 AC 150 ms
10,112 KB
testcase_14 AC 323 ms
16,132 KB
testcase_15 AC 304 ms
15,468 KB
testcase_16 AC 122 ms
9,088 KB
testcase_17 AC 253 ms
13,920 KB
testcase_18 AC 89 ms
7,552 KB
testcase_19 AC 307 ms
15,852 KB
testcase_20 AC 148 ms
9,984 KB
testcase_21 AC 167 ms
10,752 KB
testcase_22 AC 163 ms
10,496 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 3 ms
6,676 KB
testcase_28 AC 336 ms
16,868 KB
testcase_29 AC 353 ms
16,868 KB
testcase_30 AC 337 ms
16,868 KB
testcase_31 AC 336 ms
16,868 KB
testcase_32 AC 335 ms
16,868 KB
testcase_33 AC 480 ms
51,044 KB
testcase_34 AC 110 ms
17,476 KB
testcase_35 AC 581 ms
16,868 KB
testcase_36 TLE -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

template <class T> struct Manager {
    T lo_sum = 0;
    T hi_sum = 0;

    multiset<T> lo; // size(lo) >= size(hi) holds
    multiset<T> hi;

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

    int size() const { return lo.size() + hi.size(); }

    T min() const {
        assert(!empty());
        return *lo.begin();
    }

    T max() const {
        assert(!empty());
        return hi.empty() ? *lo.rbegin() : *hi.rbegin();
    }

    T median_floor() const {
        assert(!empty());
        return *lo.rbegin();
    }

    T median_ceil() const {
        assert(!empty());
        return lo.size() == hi.size() ? *hi.begin() : *lo.rbegin();
    }

    T solve() const {
        if (size() <= 1) return 0;

        const int nlo = lo.size();
        const int nhi = hi.size();

        T med = *lo.rbegin();
        T ret = hi_sum - med * nhi + med * nlo - lo_sum;
        return ret;
    }

    void balance() {

        while (!lo.empty() and !hi.empty() and *lo.rbegin() > *hi.begin()) {
            T max_lo = *lo.rbegin();
            T min_hi = *hi.begin();
            lo.erase(prev(lo.end()));
            hi.erase(hi.begin());
            lo.insert(min_hi);
            hi.insert(max_lo);
            lo_sum += min_hi - max_lo;
            hi_sum += max_lo - min_hi;
        }

        while (lo.size() > hi.size() + 1) {
            hi.insert(*lo.rbegin());
            hi_sum += *lo.rbegin();
            lo_sum -= *lo.rbegin();
            lo.erase(prev(lo.end()));
        }

        while (hi.size() > lo.size()) {
            lo.insert(*hi.begin());
            lo_sum += *hi.begin();
            hi_sum -= *hi.begin();
            hi.erase(hi.begin());
        }
    }

    void add(T x) {
        if (lo.empty() or x <= *lo.rbegin()) {
            lo.insert(x);
            lo_sum += x;
        } else {
            hi.insert(x);
            hi_sum += x;
        }
        balance();
    }

    void erase(T x) {
        if (lo.count(x)) {
            lo.erase(lo.find(x));
            lo_sum -= x;
        } else if (hi.count(x)) {
            hi.erase(hi.find(x));
            hi_sum -= x;
        } else {
            return;
        }
        balance();
    }
};

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int N;
    cin >> N;
    vector<int> A(N + 1);
    for (auto &a : A) cin >> a;

    vector<vector<int>> to(N + 1);

    for (int e = 0; e < N; ++e) {
        int a, b;
        cin >> a >> b;
        to.at(a).push_back(b);
        to.at(b).push_back(a);
    }

    Manager<long long> manager;

    vector<long long> ret(N, -1);

    auto rec = [&](auto &&self, int now, int prv) -> void {
        manager.add(A.at(now));

        if (now) {
            if (manager.size() <= 2) {
                ret.at(now - 1) = -1;
            } else {
                int max = manager.max();
                int min = manager.min();
                if (max == min) {
                    ret.at(now - 1) = 1;
                } else {
                    manager.erase(max);
                    auto tmp = manager.solve();
                    manager.add(max);
                    manager.erase(min);
                    tmp = std::min(tmp, manager.solve());
                    manager.add(min);
                    ret.at(now - 1) = tmp;
                }
            }
        }

        for (int nxt : to.at(now)) {
            if (nxt == prv) continue;
            self(self, nxt, now);
        }

        manager.erase(A.at(now));
    };

    rec(rec, 0, -1);

    for (auto r : ret) cout << r << '\n';
}
0