QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#184041#5665. AA Country and King Dreamoonucup-team1209#TL 1ms8152kbC++202.2kb2023-09-20 10:52:292023-09-20 10:52:29

Judging History

你现在查看的是最新测评结果

  • [2023-09-20 10:52:29]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:8152kb
  • [2023-09-20 10:52:29]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,y,x) for (int i=(y);i>=(x);i--)
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
#define templ template<typename T>
templ bool chkmin(T &x,T y){return x>y?x=y,1:0;}
templ bool chkmax(T &x,T y){return x<y?x=y,1:0;}
void file() {
    #ifdef zqj
    freopen("a.in","r",stdin);
    #endif
}
typedef long long ll;
#define sz 603030

int n;
int a[sz];

int fa[sz],dep[sz];
int vis[sz];
int lca(int x,int y) {
    if (dep[x]<dep[y]) swap(x,y);
    while (dep[x]>dep[y]) x=fa[x];
    while (x!=y) x=fa[x],y=fa[y];
    return dep[x];
}

void work() {
    cin>>n;
    rep(i,1,2*n-1) cin>>a[i]; a[1]=a[2*n-1]=1;
    rep(i,1,n) vis[i]=0;
    rep(i,1,2*n-1) vis[a[i]]=1;
    int L=n*10,R=-1;
    rep(i,1,2*n-1) if (!a[i]) chkmin(L,i),chkmax(R,i);
    if (R==-1) {
        rep(i,1,n*2-1) printf("%d%c",a[i]," \n"[i==2*n-1]);
        return;
    }
    dep[1]=1; int x=1;
    rep(i,2,L-1)
        if (a[i]==fa[x]) x=fa[x];
        else fa[a[i]]=x,dep[a[i]]=dep[x]+1,x=a[i];
    x=1;
    drep(i,2*n-2,R+1)
        if (a[i]==fa[x]) x=fa[x];
        else fa[a[i]]=x,dep[a[i]]=dep[x]+1,x=a[i];
    int d=lca(a[L-1],a[R+1]);
    vector<int>V,V2;
    for (x=a[L-1];dep[x]!=d;x=fa[x]) V.push_back(x);
    V.push_back(x);
    for (int y=a[R+1];dep[y]!=d;y=fa[y]) V2.push_back(y);
    reverse(V2.begin(),V2.end());
    for (auto t:V2) V.push_back(t);
    rep(i,0,(int)V.size()-2) fa[V[i]]=V[i+1];
    queue<int>q;
    rep(i,1,n) if (!vis[i]) q.push(i);
    x=V[0]; int pos=L;
    while (233) {
        if (pos==R+1) break;
        if (x==V.back()) {
            fa[q.front()]=x; x=a[pos]=q.front(); ++pos;
            q.pop();
        }
        else if (!q.size()) {
            x=a[pos]=fa[x]; ++pos;
        }
        else if (q.front()<fa[x]) {
            fa[q.front()]=x; x=a[pos]=q.front(); ++pos;
            q.pop();
        }
        else {
            x=a[pos]=fa[x]; ++pos;
        }
    }
    rep(i,1,2*n-1) printf("%d%c",a[i]," \n"[i==2*n-1]);
}

int main() {
    file();
    ios::sync_with_stdio(false),cin.tie(0);
    int T; cin>>T;
    while (T--) work();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 8152kb

input:

9
5
1 2 3 2 0 2 1 5 1
5
1 2 3 0 0 2 1 5 1
5
1 2 0 0 0 2 1 5 1
5
1 2 0 0 0 0 1 5 1
5
1 0 0 0 0 0 1 5 1
5
1 0 0 0 0 0 0 5 1
5
1 0 0 0 0 0 0 0 1
5
1 0 0 0 0 0 0 0 0
5
0 0 0 0 0 0 0 0 0

output:

1 2 3 2 4 2 1 5 1
1 2 3 2 4 2 1 5 1
1 2 3 2 4 2 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1

result:

ok 9 lines

Test #2:

score: -100
Time Limit Exceeded

input:

28668
2
0 2 1
2
0 0 1
2
0 0 0
2
1 0 1
2
1 0 0
2
1 2 0
3
0 2 1 3 1
3
0 0 1 3 1
3
0 0 0 3 1
3
0 0 0 0 1
3
0 0 0 0 0
3
1 0 1 3 1
3
1 0 0 3 1
3
1 0 0 0 1
3
1 0 0 0 0
3
1 2 0 3 1
3
1 2 0 0 1
3
1 2 0 0 0
3
1 2 1 0 1
3
1 2 1 0 0
3
1 2 1 3 0
3
0 2 3 2 1
3
0 0 3 2 1
3
0 0 0 2 1
3
1 0 3 2 1
3
1 0 0 2 1
3
1 2 ...

output:


result: