結果

問題 No.2654 [Cherry 6th Tune] Re: start! (Black Sheep)
ユーザー 👑 hitonanodehitonanode
提出日時 2024-03-10 18:07:22
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 446 ms / 7,000 ms
コード長 3,190 bytes
コンパイル時間 1,129 ms
コンパイル使用メモリ 103,780 KB
実行使用メモリ 51,044 KB
最終ジャッジ日時 2024-03-10 18:07:35
合計ジャッジ時間 12,793 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 221 ms
13,184 KB
testcase_04 AC 210 ms
12,612 KB
testcase_05 AC 126 ms
8,960 KB
testcase_06 AC 32 ms
6,676 KB
testcase_07 AC 95 ms
8,064 KB
testcase_08 AC 132 ms
9,600 KB
testcase_09 AC 226 ms
13,324 KB
testcase_10 AC 31 ms
6,676 KB
testcase_11 AC 149 ms
10,496 KB
testcase_12 AC 25 ms
6,676 KB
testcase_13 AC 142 ms
10,112 KB
testcase_14 AC 293 ms
16,132 KB
testcase_15 AC 275 ms
15,468 KB
testcase_16 AC 117 ms
9,088 KB
testcase_17 AC 232 ms
13,920 KB
testcase_18 AC 81 ms
7,552 KB
testcase_19 AC 304 ms
15,852 KB
testcase_20 AC 139 ms
9,984 KB
testcase_21 AC 153 ms
10,752 KB
testcase_22 AC 145 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 316 ms
16,868 KB
testcase_29 AC 333 ms
16,868 KB
testcase_30 AC 321 ms
16,868 KB
testcase_31 AC 332 ms
16,868 KB
testcase_32 AC 318 ms
16,868 KB
testcase_33 AC 446 ms
51,044 KB
testcase_34 AC 110 ms
17,476 KB
testcase_35 AC 258 ms
16,868 KB
testcase_36 AC 261 ms
51,044 KB
testcase_37 AC 365 ms
16,868 KB
testcase_38 AC 425 ms
51,044 KB
testcase_39 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


using lint = long long;

struct Manager {
    lint lo_sum = 0;
    lint hi_sum = 0;

    multiset<int> lo;
    multiset<int> hi;

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

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

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

    void balance() {

        while (!lo.empty() and !hi.empty() and *lo.rbegin() > *hi.begin()) {
            int max_lo = *lo.rbegin();
            int 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(int x) {
        if (lo.empty() or x <= *lo.rbegin()) {
            lo.insert(x);
            lo_sum += x;
        } else {
            hi.insert(x);
            hi_sum += x;
        }
        balance();
    }

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

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

    int min() const { return *lo.begin(); }

    int max() const { return *hi.rbegin(); }
};

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 manager;

    vector<lint> 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);
                    lint 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