結果
問題 | No.2304 Distinct Elements |
ユーザー | t98slider |
提出日時 | 2023-05-13 20:40:31 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,720 bytes |
コンパイル時間 | 2,112 ms |
コンパイル使用メモリ | 187,636 KB |
実行使用メモリ | 34,936 KB |
最終ジャッジ日時 | 2024-11-29 11:58:30 |
合計ジャッジ時間 | 7,376 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 1 ms
6,820 KB |
testcase_02 | AC | 2 ms
6,820 KB |
testcase_03 | AC | 2 ms
6,820 KB |
testcase_04 | AC | 105 ms
34,900 KB |
testcase_05 | AC | 105 ms
34,644 KB |
testcase_06 | AC | 107 ms
34,644 KB |
testcase_07 | AC | 106 ms
34,772 KB |
testcase_08 | AC | 104 ms
34,772 KB |
testcase_09 | AC | 78 ms
31,380 KB |
testcase_10 | AC | 78 ms
31,388 KB |
testcase_11 | AC | 79 ms
31,336 KB |
testcase_12 | AC | 85 ms
31,376 KB |
testcase_13 | AC | 80 ms
31,448 KB |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | AC | 2 ms
6,820 KB |
testcase_21 | AC | 2 ms
6,820 KB |
testcase_22 | WA | - |
testcase_23 | AC | 2 ms
6,820 KB |
testcase_24 | AC | 2 ms
6,820 KB |
testcase_25 | AC | 2 ms
6,820 KB |
testcase_26 | AC | 2 ms
6,820 KB |
testcase_27 | AC | 1 ms
6,816 KB |
testcase_28 | AC | 2 ms
6,820 KB |
testcase_29 | AC | 84 ms
26,124 KB |
testcase_30 | AC | 8 ms
6,820 KB |
testcase_31 | AC | 70 ms
22,532 KB |
testcase_32 | AC | 63 ms
25,392 KB |
testcase_33 | AC | 6 ms
6,816 KB |
testcase_34 | AC | 65 ms
25,772 KB |
testcase_35 | AC | 71 ms
28,588 KB |
testcase_36 | AC | 6 ms
6,824 KB |
testcase_37 | AC | 34 ms
12,568 KB |
testcase_38 | AC | 112 ms
34,120 KB |
testcase_39 | AC | 110 ms
34,616 KB |
testcase_40 | AC | 67 ms
23,796 KB |
testcase_41 | AC | 34 ms
13,848 KB |
testcase_42 | AC | 95 ms
34,644 KB |
testcase_43 | AC | 107 ms
34,644 KB |
testcase_44 | AC | 57 ms
23,348 KB |
testcase_45 | WA | - |
testcase_46 | WA | - |
testcase_47 | AC | 2 ms
6,820 KB |
testcase_48 | AC | 2 ms
6,820 KB |
testcase_49 | AC | 115 ms
34,772 KB |
testcase_50 | AC | 117 ms
34,644 KB |
testcase_51 | AC | 109 ms
34,772 KB |
testcase_52 | AC | 101 ms
34,768 KB |
testcase_53 | AC | 86 ms
32,060 KB |
testcase_54 | AC | 98 ms
34,008 KB |
testcase_55 | AC | 101 ms
34,936 KB |
testcase_56 | AC | 101 ms
34,768 KB |
testcase_57 | WA | - |
testcase_58 | AC | 102 ms
34,768 KB |
testcase_59 | WA | - |
testcase_60 | AC | 2 ms
6,820 KB |
testcase_61 | AC | 2 ms
6,816 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]); swap(i, 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; i < n; i++){ b[i] = a[i] - i; pq[i].push(b[i]); st.emplace(i); } for(int i = 0; i + 1 < n; i++){ if(b[i] == b[i + 1]) merge(i, i + 1); } while(st.size() >= 2){ int i = uf.leader(st.top()); st.pop(); int j = uf.leader(st.top()); 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'; }