답안 #75186

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
75186 2018-09-08T16:15:13 Z vex 중앙값 배열 (balkan11_medians) C++14
90 / 100
16 ms 1564 KB
#include <bits/stdc++.h>
#define maxn 100005
using namespace std;

int n;
bool bio[maxn];
int sol[2*maxn];
int tree[2*maxn];
int najm,najv;

int query(int k) {
    int s = 0;
    while (k >= 1) {
        s += tree[k];
        k -= k&-k;
    }
    return s;
}
void update(int k) {
    while (k <= 2*n-1) {
        tree[k] ++;
        k += k&-k;
    }
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    cin>>n;
    najm=1;
    najv=2*n-1;
    for(int i=1;i<=2*n-1;i++)bio[i]=false;

    cin>>sol[0];
    bio[sol[0]]=true;
    update(sol[0]);
    for(int i=1;i<n;i++)
    {
        int x;
        cin>>x;

        int uk=2*i-1;
        int man=query(x-1);
        int vec=uk-man;

        if(!bio[x])
        {
            sol[uk]=x;
            uk++;
            bio[x]=true;
            update(x);
            if(vec>man)
            {
                while(bio[najm])najm++;
                sol[uk]=najm;
                update(najm);
                bio[najm]=true;
                najm++;
                uk++;
            }
            else{
                while(bio[najv])najv--;
                sol[uk]=najv;
                update(najv);
                bio[najv]=true;
                najv--;
                uk++;
            }
        }
        else{
            for(int j=0;j<2;j++)
            {
                if(vec>man)
                {
                    while(bio[najm])najm++;
                    sol[uk]=najm;
                    update(najm);
                    bio[najm]=true;
                    najm++;
                    man++;
                    uk++;
                }
                else{
                    while(bio[najv])najv--;
                    sol[uk]=najv;
                    update(najv);
                    bio[najv]=true;
                    najv--;
                    vec++;
                    uk++;
                }
            }
        }
    }

    for(int i=0;i<2*n-1;i++)cout<<sol[i]<<" ";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 480 KB Output is correct
3 Correct 2 ms 480 KB Output is correct
4 Correct 2 ms 480 KB Output is correct
5 Correct 2 ms 484 KB Output is correct
6 Correct 2 ms 484 KB Output is correct
7 Correct 2 ms 484 KB Output is correct
8 Correct 2 ms 488 KB Output is correct
9 Correct 2 ms 612 KB Output is correct
10 Correct 2 ms 612 KB Output is correct
11 Correct 2 ms 688 KB Output is correct
12 Correct 2 ms 688 KB Output is correct
13 Correct 2 ms 688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 688 KB Output is correct
2 Correct 3 ms 740 KB Output is correct
3 Correct 6 ms 864 KB Output is correct
4 Correct 8 ms 1028 KB Output is correct
5 Correct 16 ms 1564 KB Output is correct
6 Runtime error 3 ms 1564 KB Execution killed with signal 11 (could be triggered by violating memory limits)
7 Runtime error 3 ms 1564 KB Execution killed with signal 11 (could be triggered by violating memory limits)