Submission #42482

# Submission time Handle Problem Language Result Execution time Memory
42482 2018-02-27T16:37:43 Z Hassoony medians (balkan11_medians) C++14
100 / 100
158 ms 15540 KB
#include<bits/stdc++.h>
#include<unordered_map>
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef long double D;
const ll inf=(1ll<<61);
const ll mod=1e9+7;
const int MX=2e5+9;
int n,a[MX],b[MX],bit[MX],vis[MX];
void add(int x){
    while(x<MX){
        bit[x]++;
        x+=x&-x;
    }
}
int get(int x){
    int ret=0;
    while(x){
        ret+=bit[x];
        x-=x&-x;
    }
    return ret;
}
set<int>s;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        scanf("%d",&b[i]);
    }
    for(int i=1;i<=2*n-1;i++)s.insert(i);
    add(b[1]);
    a[1]=b[1];
    s.erase(b[1]);
    vis[b[1]]=1;
    int j=2;
    for(int i=2;i<=n;i++){
        if(!vis[b[i]]){
            a[j]=b[i];
            add(b[i]);
            s.erase(b[i]);
            int x=get(b[i]-1);
            int y=get(MX)-get(b[i]);
            if(x>y){
                a[j+1]=*s.rbegin();s.erase(a[j+1]);
            }
            else a[j+1]=*s.begin();s.erase(a[j+1]);
            add(a[j+1]);
            vis[a[j]]=vis[a[j+1]]=1;
            j+=2;
            continue;
        }
        int x=get(b[i]-1);
        int y=get(MX)-get(b[i]);
        if(x==y){
            a[j]=*s.begin();s.erase(s.begin());
            add(a[j]);
            a[j+1]=*s.rbegin();s.erase(a[j+1]);
            add(a[j+1]);
            j+=2;
        }
        else if(x>y){
            a[j]=*s.rbegin();s.erase(a[j]);
            add(a[j]);
            a[j+1]=*s.rbegin();s.erase(a[j+1]);
            add(a[j+1]);
            j+=2;
        }
        else{
            a[j]=*s.begin();s.erase(s.begin());
            add(a[j]);
            a[j+1]=*s.begin();s.erase(s.begin());
            add(a[j+1]);
            j+=2;
        }
        vis[a[j-1]]=vis[a[j-2]]=1;
    }
    for(int i=1;i<=2*n-1;i++)cout<<a[i]<<" ";
}

Compilation message

medians.cpp: In function 'int main()':
medians.cpp:30:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&b[i]);
                          ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 484 KB Output is correct
3 Correct 2 ms 592 KB Output is correct
4 Correct 2 ms 708 KB Output is correct
5 Correct 2 ms 708 KB Output is correct
6 Correct 2 ms 708 KB Output is correct
7 Correct 2 ms 708 KB Output is correct
8 Correct 2 ms 724 KB Output is correct
9 Correct 2 ms 728 KB Output is correct
10 Correct 2 ms 732 KB Output is correct
11 Correct 2 ms 804 KB Output is correct
12 Correct 2 ms 936 KB Output is correct
13 Correct 2 ms 936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1040 KB Output is correct
2 Correct 5 ms 1324 KB Output is correct
3 Correct 10 ms 1876 KB Output is correct
4 Correct 17 ms 3084 KB Output is correct
5 Correct 36 ms 5356 KB Output is correct
6 Correct 81 ms 10028 KB Output is correct
7 Correct 158 ms 15540 KB Output is correct