QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#510163 | #6301. Minimum Suffix | yyyyxh | WA | 1ms | 9776kb | C++14 | 1.5kb | 2024-08-08 21:42:43 | 2024-08-08 21:42:43 |
Judging History
answer
#include <cstdio>
#include <algorithm>
using namespace std;
int read(){
char c=getchar();int x=0;
while(c<48||c>57) c=getchar();
do x=x*10+(c^48),c=getchar();
while(c>=48&&c<=57);
return x;
}
const int N=3000003;
int p[N],n;
int stk[N],tp;
bool eq[N];int las[N];
int a[N];
void solve(){
n=read();
for(int i=1;i<=n;++i) p[i]=read();
tp=0;
for(int i=1;i<=n;++i){
stk[++tp]=i;
while(tp&&stk[tp]>p[i]) --tp;
if(!tp||stk[tp]!=p[i]){puts("-1");return;}
}
stk[tp+1]=n+1;
for(int i=1;i<=tp;++i){
int ps=stk[i];
for(int x=stk[i]+1;x<stk[i+1];++x){
las[x]=ps;
if(p[x]==stk[i]){eq[x]=0,ps=p[x];continue;}
if(p[x]-x==p[ps]-ps){eq[x]=1,++ps;continue;}
puts("-1");return;
}
}
for(int i=tp;i;--i){
a[stk[i]]=1;
for(int x=stk[i]+1;x<stk[i+1];++x){
if(eq[x]) a[x]=a[las[x]];
else a[x]=a[las[x]]+1;
}
if(i<tp){
int cmp=0,ps=0;
for(int x=stk[i],y=stk[i+1];x<stk[i+1]&&y<stk[i+2];++x,++y){
if(a[x]>a[y]){cmp=1;ps=x;break;}
if(a[x]<a[y]){cmp=-1;ps=x;break;}
}
if(!cmp){
if(stk[i+1]-stk[i]>stk[i+2]-stk[i+1]) cmp=1;
if(stk[i+1]-stk[i]<stk[i+2]-stk[i+1]) cmp=-1,ps=stk[i+1]-1;
}
if(cmp>=0) continue;
while(ps>stk[i]&&eq[ps]) --ps;
a[ps]=a[ps-stk[i]+stk[i+1]]+1;
for(int x=ps+1;x<stk[i+1];++x){
if(eq[x]) a[x]=a[las[x]];
else a[x]=a[las[x]]+1;
}
}
}
for(int i=1;i<=n;++i) printf("%d ",a[i]);
putchar('\n');
}
int main(){
int tc=read();
while(tc--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9664kb
input:
6 3 1 1 1 3 1 1 2 3 1 1 3 3 1 2 1 3 1 2 2 3 1 2 3
output:
1 2 2 -1 1 2 1 1 1 2 2 1 2 1 1 1
result:
ok 16 numbers
Test #2:
score: 0
Accepted
time: 1ms
memory: 9776kb
input:
2 2 1 1 2 1 2
output:
1 2 1 1
result:
ok 4 number(s): "1 2 1 1"
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 9680kb
input:
24 4 1 1 1 1 4 1 1 1 2 4 1 1 1 3 4 1 1 1 4 4 1 1 2 1 4 1 1 2 2 4 1 1 2 3 4 1 1 2 4 4 1 1 3 1 4 1 1 3 2 4 1 1 3 3 4 1 1 3 4 4 1 2 1 1 4 1 2 1 2 4 1 2 1 3 4 1 2 1 4 4 1 2 2 1 4 1 2 2 2 4 1 2 2 3 4 1 2 2 4 4 1 2 3 1 4 1 2 3 2 4 1 2 3 3 4 1 2 3 4
output:
1 2 2 2 -1 -1 1 2 2 1 -1 -1 -1 -1 1 2 1 3 -1 1 2 1 2 1 2 1 1 1 1 2 2 -1 -1 1 1 2 1 -1 2 1 2 2 -1 2 1 2 1 1 1 1 2 2 1 1 2 3 2 1 2 1 1 1 1
result:
wrong answer 56th numbers differ - expected: '2', found: '3'