結果
問題 | No.2654 [Cherry 6th Tune] Re: start! (Black Sheep) |
ユーザー | 👑 AngrySadEight |
提出日時 | 2024-01-05 17:48:30 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,570 bytes |
コンパイル時間 | 5,979 ms |
コンパイル使用メモリ | 231,632 KB |
実行使用メモリ | 54,956 KB |
最終ジャッジ日時 | 2024-09-27 19:08:41 |
合計ジャッジ時間 | 25,639 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 3 ms
6,816 KB |
testcase_02 | AC | 3 ms
6,944 KB |
testcase_03 | AC | 528 ms
16,768 KB |
testcase_04 | AC | 481 ms
16,000 KB |
testcase_05 | AC | 280 ms
10,880 KB |
testcase_06 | AC | 79 ms
6,940 KB |
testcase_07 | AC | 228 ms
9,600 KB |
testcase_08 | WA | - |
testcase_09 | AC | 523 ms
17,004 KB |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | AC | 342 ms
12,416 KB |
testcase_14 | WA | - |
testcase_15 | AC | 633 ms
19,852 KB |
testcase_16 | AC | 278 ms
11,008 KB |
testcase_17 | AC | 566 ms
17,776 KB |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | AC | 353 ms
13,056 KB |
testcase_23 | WA | - |
testcase_24 | AC | 3 ms
6,944 KB |
testcase_25 | WA | - |
testcase_26 | AC | 3 ms
6,940 KB |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | AC | 709 ms
21,732 KB |
testcase_30 | AC | 711 ms
21,744 KB |
testcase_31 | WA | - |
testcase_32 | AC | 714 ms
21,740 KB |
testcase_33 | AC | 890 ms
54,856 KB |
testcase_34 | AC | 464 ms
22,132 KB |
testcase_35 | AC | 756 ms
21,704 KB |
testcase_36 | AC | 762 ms
54,956 KB |
testcase_37 | AC | 755 ms
21,808 KB |
testcase_38 | AC | 800 ms
54,900 KB |
testcase_39 | AC | 2 ms
6,944 KB |
ソースコード
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <algorithm> #include <atcoder/all> #include <bitset> #include <cassert> #include <cmath> #include <ctime> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using namespace atcoder; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repr(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define repk(i, k, n) for (int i = k; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define mod1 1000000007 #define mod2 998244353 #define mod3 100000007 #define vi vector<int> #define vs vector<string> #define vc vector<char> #define vl vector<ll> #define vb vector<bool> #define vvi vector<vector<int>> #define vvc vector<vector<char>> #define vvl vector<vector<ll>> #define vvb vector<vector<bool>> #define vvvi vector<vector<vector<int>>> #define vvvl vector<vector<vector<ll>>> #define pii pair<int, int> #define pil pair<int, ll> #define pli pair<ll, int> #define pll pair<ll, ll> #define vpii vector<pair<int, int>> #define vpll vector<pair<ll, ll>> #define vvpii vector<vector<pair<int, int>>> #define vvpll vector<vector<pair<ll, ll>>> using mint = modint998244353; template <typename T> void debug(T e) { cerr << e << endl; } template <typename T> void debug(vector<T> &v) { rep(i, v.size()) { cerr << v[i] << " "; } cerr << endl; } template <typename T> void debug(vector<vector<T>> &v) { rep(i, v.size()) { rep(j, v[i].size()) { cerr << v[i][j] << " "; } cerr << endl; } } template <typename T> void debug(vector<pair<T, T>> &v) { rep(i, v.size()) { cerr << v[i].first << " " << v[i].second << endl; } } template <typename T> void debug(set<T> &st) { for (auto itr = st.begin(); itr != st.end(); itr++) { cerr << *itr << " "; } cerr << endl; } template <typename T> void debug(multiset<T> &ms) { for (auto itr = ms.begin(); itr != ms.end(); itr++) { cerr << *itr << " "; } cerr << endl; } template <typename T> void debug(map<T, T> &mp) { for (auto itr = mp.begin(); itr != mp.end(); itr++) { cerr << itr->first << " " << itr->second << endl; } } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << H << " "; debug_out(T...); } ll my_pow(ll x, ll n, ll mod) { // 繰り返し二乗法.x^nをmodで割った余り. ll ret; if (n == 0) { ret = 1; } else if (n % 2 == 1) { ret = (x * my_pow((x * x) % mod, n / 2, mod)) % mod; } else { ret = my_pow((x * x) % mod, n / 2, mod); } return ret; } ll inv(ll x, ll mod) { return my_pow(x, mod - 2, mod); } struct BMS { // 2 個の multiset を用いて中央値を取得する機構 multiset<ll> ms1; multiset<ll> ms2; ll sum1 = 0; ll sum2 = 0; void balance() { if (ms1.size() < ms2.size()) { auto itr = ms2.begin(); ms2.erase(itr); sum2 -= *itr; ms1.insert(*itr); sum1 += *itr; } if (ms1.size() >= ms2.size() + 2) { auto itr = ms1.end(); itr--; ms1.erase(itr); sum1 -= *itr; ms2.insert(*itr); sum2 += *itr; } } void insert(ll v) { if (v >= *(ms2.begin())) { ms2.insert(v); sum2 += v; } else { ms1.insert(v); sum1 += v; } balance(); } void erase(ll v) { // 「ひとつだけ」削除する if (ms1.find(v) != ms1.end()) { ms1.erase(ms1.find(v)); sum1 -= v; } else if (ms2.find(v) != ms2.end()) { ms2.erase(ms2.find(v)); sum2 -= v; } else { return; } balance(); } ll get_size() { ll siz1 = ms1.size(); ll siz2 = ms2.size(); return siz1 + siz2; } ll get_mean() { ll ret; if (ms1.size() > ms2.size()) { ret = *(ms1.rbegin()); } else { ll ms1max = *(ms1.rbegin()); ll ms2min = *(ms2.begin()); ret = (ms1max + ms2min) / 2; } return ret; } ll get_score() { ll mean = get_mean(); ll score = (mean * ms1.size() - sum1) + (sum2 - mean * ms2.size()); return score; } ll get_min_val() { return *(ms1.begin()); } ll get_max_val() { return *(ms2.rbegin()); } }; ll INF = 1000000000000000002; void dfs(vector<vector<ll>> &graph, vector<bool> &isvisited, vector<ll> &ans, BMS &bms, vector<ll> &A, ll v) { isvisited[v] = true; bms.insert(A[v]); // この状態で問題を解く if (bms.get_size() <= 2) { ans[v] = -1; } else { ll anssub = INF; ll min_val = bms.get_min_val(); bms.erase(min_val); ll min_mean = bms.get_mean(); if (min_mean != min_val) { ll min_score = bms.get_score(); anssub = min(anssub, min_score); } bms.insert(min_val); ll max_val = bms.get_max_val(); bms.erase(max_val); ll max_mean = bms.get_mean(); if (max_mean != max_val) { ll max_score = bms.get_score(); anssub = min(anssub, max_score); } bms.insert(max_val); if (min_val == max_val) { anssub = 1; } ans[v] = anssub; } for (ll i = 0; i < graph[v].size(); i++) { ll x = graph[v][i]; if (isvisited[x]) { continue; } dfs(graph, isvisited, ans, bms, A, graph[v][i]); } bms.erase(A[v]); } int main() { ll N; cin >> N; vector<ll> A(N + 1); for (ll i = 0; i < N + 1; i++) { cin >> A[i]; } vector<ll> u(N); vector<ll> v(N); for (ll i = 0; i < N; i++) { cin >> u[i] >> v[i]; } vector<vector<ll>> graph(N + 1, vector<ll>(0)); for (ll i = 0; i < N; i++) { graph[u[i]].push_back(v[i]); graph[v[i]].push_back(u[i]); } BMS bms; vector<ll> ans(N + 1, -1); vector<bool> isvisited(N + 1, false); dfs(graph, isvisited, ans, bms, A, 0); for (ll i = 1; i <= N; i++) { cout << ans[i] << endl; } }