QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#420835#1873. ArrayXZC0920WA 185ms8332kbC++141.8kb2024-05-24 22:50:432024-05-24 22:50:44

Judging History

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

  • [2024-05-24 22:50:44]
  • 评测
  • 测评结果:WA
  • 用时:185ms
  • 内存:8332kb
  • [2024-05-24 22:50:43]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int t, n, a[N], u, fa[N], r[N], c[N], cn, ct, ans[N], v, b[N], bn;
void solve() 
{
    scanf("%d", &n); u = fa[0] = bn = cn = 0; ct = 0;
    for(int i = 1; i <= n; i++) 
    {
        scanf("%d", &a[i]); fa[i] = 0, r[i] = -1;
        if(a[i] != n + 1) u = i;
    }
    if(u == 0) return printf("-1\n"), void();
    bool ok = 1;
    for(int i = 1; i <= n; i++) ok &= (a[i] == i);
    if(ok)
    {
        for(int i = 1; i <= n; i++) printf("1 ");
        printf("\n"); return;
    }
    for(int i = 0; i < u; i++) 
    {
        if(a[i] != a[i + 1]) 
        {
            fa[i] = a[i + 1]; 
            for(int j = a[i] + 1; j < a[i + 1]; j++) r[j] = i;
        }
    }
    for(int i = 1; i < a[u]; i++) if(!fa[i] && i != u) c[++cn] = i;
    for(int i = a[u]; i >= 1; i--) 
    {
        if(r[i] == -1 || r[i] == 0) continue;
        while(cn && c[cn] >= i) cn--;
        if(cn == 0) return printf("-1\n"), void();
        int j = c[cn--];
        if(j <= r[i]) return printf("-1\n"), void();
        fa[j] = i;
    }
    for(int i = u; i <= n; i++) if(r[i] == 0) b[++bn] = i;
    for(int i = 1; i < u; i++) 
    {
        if(!fa[i]) 
        {
            if(bn == 0) return printf("-1\n"), void();
            fa[i] = b[bn--];
        }
    }
    for(int i = a[u]; i >= 1; i--) 
    {
        if(!fa[i]) ans[i] = ++ct;
        else ans[i] = ans[fa[i]];
    }
    for(int i = a[u] + 1; i <= n; i++) 
    {
        if(ans[u] == 1 && ct == 1) return printf("-1\n"), void();
        ans[i] = (ans[u] == 1 ? 2 : 1);
    }
    for(int i = 1; i <= n; i++) printf("%d ", ans[i]);
    printf("\n");
}
int main()
{
    scanf("%d", &t);
    while(t--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
4
3 3 5 5
7
4 6 6 7 8 8 8
5
2 3 4 4 6

output:

2 2 1 1 
2 3 1 4 3 2 1 
-1

result:

ok 233

Test #2:

score: -100
Wrong Answer
time: 185ms
memory: 8332kb

input:

23198
7
1 2 3 4 5 6 7
7
1 2 3 4 5 6 8
7
1 2 3 4 5 7 7
7
1 2 3 4 5 7 8
7
1 2 3 4 5 8 8
7
1 2 3 4 6 6 7
7
1 2 3 4 6 6 8
7
1 2 3 4 6 7 7
7
1 2 3 4 6 7 8
7
1 2 3 4 6 8 8
7
1 2 3 4 7 7 7
7
1 2 3 4 7 7 8
7
1 2 3 4 7 8 8
7
1 2 3 4 8 8 8
7
1 2 3 5 5 6 7
7
1 2 3 5 5 6 8
7
1 2 3 5 5 7 7
7
1 2 3 5 5 7 8
7
1 2 ...

output:

1 1 1 1 1 1 1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

wrong answer oh no! -1