Submission #51631

#TimeUsernameProblemLanguageResultExecution timeMemory
51631someone_aamedians (balkan11_medians)C++17
10 / 100
77 ms6288 KiB
#include <bits/stdc++.h> #define ll long long using namespace std; const int maxn = 100100; int tree[8*maxn], b[maxn], a[2*maxn], n; bool used[2*maxn]; void update(int x, int li=0, int ri=2*n,int index=1) { if(li==ri) tree[index] = 1; else { int mid = (li+ri) /2; if(x <= mid) update(x, li, mid, 2*index); else if(x > mid) update(x, mid+1, ri, 2*index+1); } } int query(int ql, int qr, int li=0, int ri=2*n, int index=1) { if(qr < ql) return 0; if(li > qr || ri < ql) return 0; else if(li >= ql && ri <= qr) return tree[index]; else { int mid = (li+ri)/2; return query(ql,qr,li,mid,2*index) + query(ql,qr,mid+1,ri,2*index+1); } } int main() { cin>>n; for(int i=1;i<=n;i++) { cin>>b[i]; } a[1] = b[1]; used[b[1]] = true; update(b[1]); int st = 1, fh = 2*n-1; if(b[1] == 1) st = 2; else st = 1; if(b[1] == 2*n-1) fh = 2*n-2; else fh = 2*n-1; for(int i=2;i<=n;i++) { //cout<<i<<" -> "; if(used[b[i]]) { //cout<<"Placing A: "<<st<<" "<<fh<<" -> "; a[2*(i-1)] = st; a[2*(i-1)+1] = fh; update(st); update(fh); used[st] = used[fh] = true; while(used[st] && st < 2 * n) st++; while(used[fh] && fh > 0) fh--; //cout<<st<<" "<<fh<<"\n"; } else { //cout<<"Placing B: "<<st<<" "<<fh<<" -> "; a[2*(i-1)] = b[i]; used[b[i]] = true; update(b[i]); int temp = query(1, b[i]-1); while(used[st] && st < 2 * n) st++; while(used[fh] && fh > 0) fh--; if(temp == (i-1)) { // stavi pogolemo a[2*(i-1)+1] = fh; used[fh] = true; update(fh); while(used[fh] && fh > 0) fh--; } else if(temp < (i-1)) { // stavi pomalo a[2*(i-1)+1] = st; used[st] = true; update(st); while(used[st] && st < 2 * n) st++; } //cout<<st<<" "<<fh<<"\n"; } } for(int i=1;i<=2*n-1;i++) { cout<<a[i]<<" "; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...