답안 #635389

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
635389 2022-08-26T07:59:40 Z Mahdi Watermelon (INOI20_watermelon) C++17
31 / 100
111 ms 10164 KB
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define all(v) v.begin(), v.end()
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> pii;
const int N=1e5+5;
int n, b[N], k, ans[N], d[N];
vector<int>g[N], nd;
bool mk[N];

struct seg_tree{
    int sz, a[4*N];
    seg_tree(){
        sz=1;
        while(sz<n)
            sz<<=1;
        for(int i=sz-1;i<sz+n-1;++i)
            a[i]=1;
        for(int i=sz-2;i>=0;--i)
            a[i]=a[2*i+1]+a[2*i+2];
    }

    void zero(int x){
        x+=sz-1;
        a[x]=0;
        while(x){
            --x;
            x/=2;
            --a[x];
        }
    }

    int sum(int x, int lx, int rx, int l, int r){
        if(lx>=r || rx<=l)
            return 0;
        if(lx>=l && rx<=r)
            return a[x];
        int m=(lx+rx)/2;
        return sum(2*x+1, lx, m, l, r)+sum(2*x+2, m, rx, l, r);
    }
    
    int nxt(int x, int lx, int rx, int h, int t){
        if(lx==rx-1)
            return lx;
        int m=(lx+rx)/2;
        if(h>=m)
            return nxt(2*x+2, m, rx, h, t);
        if(a[2*x+2]<t)
            return nxt(2*x+1, lx, m, h, t-a[2*x+2]);
        return nxt(2*x+2, m, rx, h, t);
    }
    
    int per(int x, int lx, int rx, int h, int t){
        if(lx+1==rx)
            return lx;
        int m=(lx+rx)/2;
        if(h<m)
            return per(2*x+1, lx, m, h, t);
        if(a[2*x+1]<t)
            return per(2*x+2, m, rx, h, t-a[2*x+1]);
        return per(2*x+1, lx, m, h, t);
    }

    int nxt(int x){
        int t=sum(0, 0, sz, x+1, n);
        if(t==0)
            return -1;
        return nxt(0, 0, sz, x, t);
    }

    int per(int x){
        int t=sum(0, 0, sz, 0, x);
        if(t==0)
            return n;
        return per(0, 0, sz, x, t);
    }
};

void dfs(const int &v){
    mk[v]=1;
    for(int u: g[v]){
        if(!mk[u])
            dfs(u);
    }
    nd.push_back(v);
}

int main(){
    cin>>n>>k;
    for(int i=0;i<n;++i)
        cin>>b[i];
    int cmo=0;
    for(int i=0;i<n;++i){
        if(b[i]==-1)
            ++cmo;
    }
    if(b[n-1]!=-1){
        cout<<-1<<'\n';
        return 0;
    }
    if(n==1){
        cout<<(k==1 ? 1 : -1)<<'\n';
        return 0;
    }
    seg_tree A;
    vector<int>v;
    for(int i=0;i<n-1;++i){
        if(b[i]==1){
            g[i].push_back(i+1);
            int pp=A.per(i);
            if(pp!=n)
                v.push_back(pp);
            A.zero(i);
        }
        else{
            g[i+1].push_back(i);
        }
    }
    for(int i=2;!v.empty();++i){
        v.resize(distance(v.begin(), unique(all(v))));
        vector<int>w;
        for(int u: v){
            if(b[u]==i){
                g[u].push_back(A.nxt(u));
                A.zero(u);
                int uw=A.per(u);
                if(uw!=n)
                    w.push_back(uw);
            }
            else{
                int uw=A.nxt(u);
                if(uw!=-1)
                    g[uw].push_back(u);
            }
        }
        v=w;
    }
    if(A.sum(0, 0, A.sz, 0, n)!=cmo){
        cout<<-1<<'\n';
        return 0;
    }
    for(int i=0;i<n;++i){
        if(!mk[i])
            dfs(i);
    }
    memset(mk, 0, sizeof(mk));
    for(int u: nd){
        mk[u]=1;
        for(int w: g[u]){
            if(!mk[w]){
                cout<<-1<<'\n';
                return 0;
            }
        }
    }
    priority_queue<int>pq;
    for(int i=0;i<n;++i){
        for(int u: g[i])
            ++d[u];
    }
    for(int i=0;i<n;++i){
        if(d[i]==0){
            pq.push(-i);
        }
    }
    for(int i=0;i<n;++i){
        int u=-pq.top();
        pq.pop();
        ans[u]=i;
        for(int w: g[u]){
            --d[w];
            if(d[w]==0)
                pq.push(-w);
        }
    }
    for(int i=0;i<n;++i)
        cout<<ans[i]+1<<' ';
    cout<<'\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4308 KB Output is correct
2 Correct 3 ms 4308 KB Output is correct
3 Correct 2 ms 4308 KB Output is correct
4 Correct 2 ms 4180 KB Output is correct
5 Correct 2 ms 4180 KB Output is correct
6 Correct 3 ms 4180 KB Output is correct
7 Correct 2 ms 4180 KB Output is correct
8 Correct 2 ms 4180 KB Output is correct
9 Correct 2 ms 4180 KB Output is correct
10 Correct 2 ms 4180 KB Output is correct
11 Correct 2 ms 4180 KB Output is correct
12 Incorrect 2 ms 4308 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4308 KB Output is correct
2 Correct 3 ms 4308 KB Output is correct
3 Correct 2 ms 4308 KB Output is correct
4 Correct 2 ms 4180 KB Output is correct
5 Correct 2 ms 4180 KB Output is correct
6 Correct 3 ms 4180 KB Output is correct
7 Correct 2 ms 4180 KB Output is correct
8 Correct 2 ms 4180 KB Output is correct
9 Correct 2 ms 4180 KB Output is correct
10 Correct 2 ms 4180 KB Output is correct
11 Correct 2 ms 4180 KB Output is correct
12 Incorrect 2 ms 4308 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 7172 KB Output is correct
2 Correct 17 ms 5972 KB Output is correct
3 Correct 42 ms 7172 KB Output is correct
4 Correct 17 ms 5920 KB Output is correct
5 Correct 47 ms 7216 KB Output is correct
6 Correct 51 ms 7180 KB Output is correct
7 Correct 42 ms 7128 KB Output is correct
8 Correct 40 ms 7244 KB Output is correct
9 Correct 44 ms 7140 KB Output is correct
10 Correct 41 ms 7164 KB Output is correct
11 Correct 84 ms 10084 KB Output is correct
12 Correct 88 ms 10164 KB Output is correct
13 Correct 85 ms 10100 KB Output is correct
14 Correct 86 ms 10076 KB Output is correct
15 Correct 38 ms 7804 KB Output is correct
16 Correct 32 ms 7668 KB Output is correct
17 Correct 31 ms 7672 KB Output is correct
18 Correct 30 ms 7636 KB Output is correct
19 Correct 35 ms 7688 KB Output is correct
20 Correct 35 ms 7628 KB Output is correct
21 Correct 29 ms 7628 KB Output is correct
22 Correct 30 ms 7624 KB Output is correct
23 Correct 33 ms 7672 KB Output is correct
24 Correct 92 ms 9636 KB Output is correct
25 Correct 102 ms 9676 KB Output is correct
26 Correct 109 ms 9648 KB Output is correct
27 Correct 91 ms 9640 KB Output is correct
28 Correct 95 ms 9700 KB Output is correct
29 Correct 91 ms 9684 KB Output is correct
30 Correct 27 ms 5924 KB Output is correct
31 Correct 50 ms 6988 KB Output is correct
32 Correct 111 ms 9148 KB Output is correct
33 Correct 104 ms 9644 KB Output is correct
34 Correct 10 ms 4820 KB Output is correct
35 Correct 18 ms 5444 KB Output is correct
36 Correct 63 ms 6980 KB Output is correct
37 Correct 107 ms 9688 KB Output is correct
38 Correct 96 ms 9692 KB Output is correct
39 Correct 102 ms 9708 KB Output is correct
40 Correct 56 ms 10156 KB Output is correct
41 Correct 88 ms 9592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 7172 KB Output is correct
2 Correct 17 ms 5972 KB Output is correct
3 Correct 42 ms 7172 KB Output is correct
4 Correct 17 ms 5920 KB Output is correct
5 Correct 47 ms 7216 KB Output is correct
6 Correct 51 ms 7180 KB Output is correct
7 Correct 42 ms 7128 KB Output is correct
8 Correct 40 ms 7244 KB Output is correct
9 Correct 44 ms 7140 KB Output is correct
10 Correct 41 ms 7164 KB Output is correct
11 Correct 84 ms 10084 KB Output is correct
12 Correct 88 ms 10164 KB Output is correct
13 Correct 85 ms 10100 KB Output is correct
14 Correct 86 ms 10076 KB Output is correct
15 Correct 38 ms 7804 KB Output is correct
16 Correct 32 ms 7668 KB Output is correct
17 Correct 31 ms 7672 KB Output is correct
18 Correct 30 ms 7636 KB Output is correct
19 Correct 35 ms 7688 KB Output is correct
20 Correct 35 ms 7628 KB Output is correct
21 Correct 29 ms 7628 KB Output is correct
22 Correct 30 ms 7624 KB Output is correct
23 Correct 33 ms 7672 KB Output is correct
24 Correct 92 ms 9636 KB Output is correct
25 Correct 102 ms 9676 KB Output is correct
26 Correct 109 ms 9648 KB Output is correct
27 Correct 91 ms 9640 KB Output is correct
28 Correct 95 ms 9700 KB Output is correct
29 Correct 91 ms 9684 KB Output is correct
30 Correct 27 ms 5924 KB Output is correct
31 Correct 50 ms 6988 KB Output is correct
32 Correct 111 ms 9148 KB Output is correct
33 Correct 104 ms 9644 KB Output is correct
34 Correct 10 ms 4820 KB Output is correct
35 Correct 18 ms 5444 KB Output is correct
36 Correct 63 ms 6980 KB Output is correct
37 Correct 107 ms 9688 KB Output is correct
38 Correct 96 ms 9692 KB Output is correct
39 Correct 102 ms 9708 KB Output is correct
40 Correct 56 ms 10156 KB Output is correct
41 Correct 88 ms 9592 KB Output is correct
42 Incorrect 45 ms 7176 KB Output isn't correct
43 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4308 KB Output is correct
2 Correct 3 ms 4308 KB Output is correct
3 Correct 2 ms 4308 KB Output is correct
4 Correct 2 ms 4180 KB Output is correct
5 Correct 2 ms 4180 KB Output is correct
6 Correct 3 ms 4180 KB Output is correct
7 Correct 2 ms 4180 KB Output is correct
8 Correct 2 ms 4180 KB Output is correct
9 Correct 2 ms 4180 KB Output is correct
10 Correct 2 ms 4180 KB Output is correct
11 Correct 2 ms 4180 KB Output is correct
12 Incorrect 2 ms 4308 KB Output isn't correct
13 Halted 0 ms 0 KB -