QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#89716#5665. AA Country and King DreamoonAlanSPWA 45ms3728kbC++141.7kb2023-03-20 23:30:502023-03-20 23:30:51

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-20 23:30:51]
  • 评测
  • 测评结果:WA
  • 用时:45ms
  • 内存:3728kb
  • [2023-03-20 23:30:50]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=6e5+9;
int a[N],T,stk[N],top,n,cnt,p;
deque<int> chd;
bool vis[N],ichd[N];

inline bool check(int u,int fa)
{
    if(u==1&&!chd.empty()) return stk[top]<chd[0];
    else return stk[top]<fa;
}

void dfs(int u,int fa)
{
    // system("pause");
    tp:;
    if(p==n) return;
    if(a[p+1]==fa) return ++p,void();
    if(a[p+1]) {dfs(a[++p],u);goto tp;}
    if(top&&(check(u,fa)||2*top-1==cnt))
    {
        a[++p]=stk[top--];
        cnt--;
        dfs(a[p],u);
        goto tp;
    }
    else
    {
        cnt--;
        if(u==1)
        {
            // if(chd[0]==a[p+2]) a[++p]=chd[1];
            // else 
            a[++p]=chd[0];
            // chd.pop_front();
            // printf("%d %d\n",u,a[p]);
            dfs(a[p],u);
            goto tp;
        }
        else a[++p]=fa;
        return;
    }
    // printf("%d %d %d\n",u,fa,p);
}

inline void solve()
{
    chd.clear();
    scanf("%d",&n);top=cnt=0;p=1;
    int bn=n;
    n=2*n-1;
    for(int i=1;i<=n;i++) vis[i]=ichd[i]=false;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),cnt+=(a[i]==0);
    for(int i=1;i<=n;i++) vis[a[i]]=true;
    for(int i=bn;i>=2;i--) if(!vis[i]) stk[++top]=i;
    // for(int i=1;i<=n;i++) vis[a[i]]
    if(!a[1]) a[1]=1,cnt--;
    for(int i=1;i<=n;i++) 
        if(!ichd[a[i]]&&(a[i+1]==1)&&a[i]) 
            ichd[a[i]]=true;
    for(int i=1;i<=n;i++)
        if(ichd[a[i]]&&a[i-1]==1)
            ichd[a[i]]=false;
    for(int i=1;i<=bn;i++) if(ichd[i]) chd.push_back(i);
    sort(chd.begin(),chd.end());
    dfs(1,300001);
    for(int i=1;i<=n;i++) printf("%d ",a[i]);
    puts("");
}

int main()
{
    scanf("%d",&T);
    while(T--) solve();
}

詳細信息

Test #1:

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

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
Wrong Answer
time: 45ms
memory: 3708kb

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:

1 2 1 
1 2 1 
1 2 1 
1 2 1 
1 2 1 
1 2 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3...

result:

wrong answer 142nd lines differ - expected: '1 2 3 2 4 2 1', found: '1 2 1 3 4 2 1 '