Submission #101221

#TimeUsernameProblemLanguageResultExecution timeMemory
101221mraronmedians (balkan11_medians)C++14
10 / 100
152 ms12252 KiB
#include<bits/stdc++.h> using namespace std; const int MAXN=200101; struct fenwick { int arr[MAXN]; fenwick() {memset(arr,0,sizeof arr);} void incr(int x, int by) { for(;x<MAXN;x+=(x&(-x))) { arr[x]+=by; } } int sum(int x) { int sum=0; for(;x>0;x-=(x&(-x))) { sum+=arr[x]; } return sum; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; vector<int> t(n); for(int i=0;i<n;++i) cin>>t[i]; set<int> s; for(int i=1;i<=2*n-1;++i) s.insert(i); fenwick fw; fw.incr(t[0], 1); s.erase(t[0]); vector<int> ans; ans.push_back(t[0]); auto med=[&]() -> int { int all=fw.sum(MAXN-1); int kell=(all+1)/2; int akt=MAXN-1; for(int i=20;i>=0;i--) { int curr=akt-(1<<i); if(curr>=1) { if(fw.sum(curr)>=kell) { akt=curr; } } } return akt; }; auto addmin=[&](){ int val=*s.begin(); s.erase(val); fw.incr(val, 1); ans.push_back(val); }; auto addmax=[&](){ auto it=s.end(); it--; int val=*it; s.erase(val); fw.incr(val, 1); ans.push_back(val); }; for(int i=1;i<n;++i) { if(s.find(t[i])==s.end()) { if(med()<t[i]) { addmin(); addmin(); }else if(med()>t[i]) { addmax(); addmax(); }else { addmin(); addmax(); } }else { s.erase(t[i]); fw.incr(t[i], 1); ans.push_back(t[i]); if(med()<=t[i]) addmin(); else addmax(); } } for(auto i:ans) { cout<<i<<" "; } cout<<"\n"; /* for(int i=0;i<(int)ans.size();i+=2) { vector<int> akt; for(int j=0;j<=i;++j) akt.push_back(ans[j]); sort(akt.begin(), akt.end()); cerr<<akt[akt.size()/2]<<" "; }cerr<<"\n";*/ return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...