結果
問題 | No.2304 Distinct Elements |
ユーザー | t98slider |
提出日時 | 2023-05-13 20:36:10 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,655 bytes |
コンパイル時間 | 2,155 ms |
コンパイル使用メモリ | 187,400 KB |
実行使用メモリ | 34,808 KB |
最終ジャッジ日時 | 2024-11-29 11:55:10 |
合計ジャッジ時間 | 8,473 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 114 ms
34,644 KB |
testcase_05 | AC | 114 ms
34,772 KB |
testcase_06 | AC | 115 ms
34,776 KB |
testcase_07 | AC | 114 ms
34,644 KB |
testcase_08 | AC | 114 ms
34,640 KB |
testcase_09 | AC | 93 ms
31,360 KB |
testcase_10 | AC | 91 ms
31,360 KB |
testcase_11 | AC | 90 ms
31,360 KB |
testcase_12 | AC | 89 ms
31,360 KB |
testcase_13 | AC | 90 ms
31,360 KB |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | AC | 2 ms
5,248 KB |
testcase_18 | WA | - |
testcase_19 | AC | 2 ms
5,248 KB |
testcase_20 | AC | 2 ms
5,248 KB |
testcase_21 | AC | 2 ms
5,248 KB |
testcase_22 | AC | 2 ms
5,248 KB |
testcase_23 | AC | 2 ms
5,248 KB |
testcase_24 | AC | 2 ms
5,248 KB |
testcase_25 | AC | 2 ms
5,248 KB |
testcase_26 | AC | 2 ms
5,248 KB |
testcase_27 | AC | 2 ms
5,248 KB |
testcase_28 | AC | 2 ms
5,248 KB |
testcase_29 | AC | 90 ms
25,996 KB |
testcase_30 | AC | 9 ms
5,248 KB |
testcase_31 | AC | 74 ms
22,536 KB |
testcase_32 | AC | 71 ms
25,216 KB |
testcase_33 | AC | 7 ms
5,248 KB |
testcase_34 | AC | 72 ms
25,836 KB |
testcase_35 | AC | 82 ms
28,628 KB |
testcase_36 | AC | 7 ms
5,248 KB |
testcase_37 | AC | 38 ms
12,572 KB |
testcase_38 | AC | 117 ms
34,116 KB |
testcase_39 | AC | 121 ms
34,620 KB |
testcase_40 | AC | 74 ms
23,732 KB |
testcase_41 | AC | 40 ms
13,852 KB |
testcase_42 | AC | 108 ms
34,768 KB |
testcase_43 | AC | 121 ms
34,640 KB |
testcase_44 | AC | 65 ms
23,552 KB |
testcase_45 | WA | - |
testcase_46 | WA | - |
testcase_47 | AC | 2 ms
5,248 KB |
testcase_48 | AC | 2 ms
5,248 KB |
testcase_49 | AC | 115 ms
34,772 KB |
testcase_50 | AC | 118 ms
34,644 KB |
testcase_51 | AC | 121 ms
34,648 KB |
testcase_52 | AC | 111 ms
34,768 KB |
testcase_53 | AC | 106 ms
34,008 KB |
testcase_54 | AC | 106 ms
34,008 KB |
testcase_55 | AC | 112 ms
34,808 KB |
testcase_56 | AC | 111 ms
34,776 KB |
testcase_57 | WA | - |
testcase_58 | AC | 114 ms
34,640 KB |
testcase_59 | WA | - |
testcase_60 | AC | 2 ms
5,248 KB |
testcase_61 | AC | 2 ms
5,248 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; struct dsu { public: int csz; dsu() : _n(0) {} dsu(int n) : _n(n), csz(n), parent_or_size(n, -1) {} int merge(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); if (x == y) return x; if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y); csz--; parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } bool same(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a && a < _n); if (parent_or_size[a] < 0) return a; return parent_or_size[a] = leader(parent_or_size[a]); } int size(int a) { assert(0 <= a && a < _n); return -parent_or_size[leader(a)]; } std::vector<std::vector<int>> groups() { std::vector<int> leader_buf(_n), group_size(_n); for (int i = 0; i < _n; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector<std::vector<int>> result(_n); for (int i = 0; i < _n; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < _n; i++) { result[leader_buf[i]].push_back(i); } result.erase( std::remove_if(result.begin(), result.end(), [&](const std::vector<int>& v) { return v.empty(); }), result.end()); return result; } private: int _n; // root node: -1 * component size // otherwise: parent std::vector<int> parent_or_size; }; template<class T> struct median_priority_queue { std::priority_queue<T> pqL; std::priority_queue<T, std::vector<T>, std::greater<T>> pqR; median_priority_queue() { pqL.emplace(std::numeric_limits<T>::min()); pqR.emplace(std::numeric_limits<T>::max()); } void push(T v){ if(v < pqL.top()) pqL.emplace(v); else pqR.emplace(v); update(); } T get(){ return pqR.top(); } int size(){ return int(pqL.size() + pqR.size()); } void update(){ while(pqL.size() + 2 <= pqR.size()){ pqL.emplace(pqR.top()); pqR.pop(); } while(pqL.size() > pqR.size()){ pqR.emplace(pqL.top()); pqL.pop(); } } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; dsu uf(n); vector<int> a(n), b(n); vector<median_priority_queue<int>> pq(n); for(auto &&v : a) cin >> v; sort(a.begin(), a.end()); auto merge = [&](int i, int j){ if(pq[i].size() < pq[j].size()) swap(pq[i], pq[j]); uf.merge(i, j); while(!pq[j].pqR.empty()){ pq[i].push(pq[j].pqR.top()); pq[j].pqR.pop(); } while(!pq[j].pqL.empty()){ pq[i].push(pq[j].pqL.top()); pq[j].pqL.pop(); } }; stack<int> st; for(int i = 0, mx = 0; i < n; i++){ b[i] = a[i] - i; pq[i].push(b[i]); st.emplace(i); } while(st.size() >= 2){ int i = uf.leader(st.top()); st.pop(); int j = uf.leader(st.top()); //cerr << pq[j].get() << " " << pq[i].get() << '\n'; if(pq[j].get() >= pq[i].get()) merge(i, j); } ll ans = 0; for(int i = 0; i < n; i++){ ans += abs(b[i] - pq[uf.leader(i)].get()); } cout << ans << '\n'; }