結果
問題 |
No.2304 Distinct Elements
|
ユーザー |
![]() |
提出日時 | 2023-05-12 22:13:16 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,030 bytes |
コンパイル時間 | 4,650 ms |
コンパイル使用メモリ | 267,996 KB |
最終ジャッジ日時 | 2025-02-12 23:01:59 |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 48 WA * 10 |
ソースコード
#include <stdio.h> #include <atcoder/all> #include <bits/stdc++.h> using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 4000000000000000001 struct absolute_minima{ using ll = long long; priority_queue<pair<ll,ll>> pQ; priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> sQ; ll pSum=0,sSum=0,pCnt=0,sCnt=0; void add(long long x,long long w){ sQ.emplace(x,w); sSum += x*w; sCnt += w; while(true){ if(pQ.size()>0&&sQ.size()>0){ if(pQ.top().first>sQ.top().first){ move(pCnt > sCnt); continue; } else if(pCnt > sCnt){ move(true); continue; } else if(pCnt + sQ.top().second <= sCnt - sQ.top().second){ move(false); continue; } } else{ if(sQ.size()==0){ move(true); continue; } else{ if(sQ.top().second <= sCnt - sQ.top().second){ move(false); continue; } } } break; } } void add(ll x){ add(x,1LL); } ll median(){ return sQ.top().first; } vector<ll> medians(){ vector<ll> ret; if((pCnt+sCnt)%2==0){ if(pCnt==sCnt)ret.push_back(pQ.top().first); else ret.push_back(sQ.top().first); } ret.push_back(median()); return ret; } ll distance_sum(){ ll ret = 0LL; ret -= pSum; ret += sSum; ret += pCnt * median(); ret -= sCnt * median(); return ret; } void move(bool f){ if(f){ if(pQ.size()>0){ pair<ll,ll> p = pQ.top(); pQ.pop(); pSum -= p.first*p.second; pCnt -= p.second; sQ.push(p); sSum += p.first*p.second; sCnt += p.second; } } else{ if(sQ.size()>0){ pair<ll,ll> p = sQ.top(); sQ.pop(); sSum -= p.first*p.second; sCnt -= p.second; pQ.push(p); pSum += p.first*p.second; pCnt += p.second; } } } int size(){ return pQ.size() + sQ.size(); } }; int main(){ int n; cin>>n; vector<int> a(n); rep(i,n)cin>>a[i]; sort(a.begin(),a.end()); vector<absolute_minima> A; rep(i,n){ absolute_minima t; t.add(a[i],1); A.push_back(t); while(A.size()>=2){ int x = A.size()-2; int y = A.size()-1; int xr = A[x].median() + A[x].sQ.size() - 1; int yl = A[y].median() - A[y].pQ.size(); if(xr<yl)break; if(A[x].size()<A[y].size())swap(A[x],A[y]); t = A.back(); A.pop_back(); while(t.pQ.size()>0){ A.back().add(t.pQ.top().first,1); t.pQ.pop(); } while(t.sQ.size()>0){ A.back().add(t.sQ.top().first,1); t.sQ.pop(); } } } long long ans = 0; rep(i,A.size()){ int l = A[i].median() - A[i].pQ.size(); //cout<<A[i].median()<<','<<A[i].pQ.size()<<','<<A[i].sQ.size()<<endl; vector<long long> vs; while(A[i].pQ.size()>0){ vs.push_back(A[i].pQ.top().first); A[i].pQ.pop(); } while(A[i].sQ.size()>0){ vs.push_back(A[i].sQ.top().first); A[i].sQ.pop(); } sort(vs.begin(),vs.end()); rep(j,vs.size()){ ans += abs(vs[j] - (l+j)); } } cout<<ans<<endl; return 0; }