QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#510145 | #6301. Minimum Suffix | yyyyxh | WA | 1ms | 9712kb | C++14 | 1.4kb | 2024-08-08 21:29:53 | 2024-08-08 21:29:53 |
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){
bool cmp=i<tp;
if(cmp) a[stk[i]]=a[stk[i+1]];
else a[stk[i]]=1;
for(int x=stk[i]+1,y=stk[i+1]+1;x<stk[i+1];++x,++y){
if(eq[x]) a[x]=a[las[x]];
else a[x]=a[las[x]]+1;
if(cmp&&y<stk[i+2]&&a[x]<a[y]){
a[x]=a[y];
int t=x;
while(true){
a[t]=a[y];
if(eq[t]) t=las[t],cmp=0;
else break;
}
a[x]=max(a[x],a[y]);
}
if(cmp&&y<stk[i+2]&&a[x]>a[y]) cmp=0;
}
if(cmp&&stk[i+2]-stk[i+1]>stk[i+1]-stk[i]){
int p=stk[i+1]-1;
while(p>stk[i]&&eq[p]) --p;
++a[p];
}
}
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: 1ms
memory: 9672kb
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: 9712kb
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: 0
Accepted
time: 1ms
memory: 9688kb
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 2 2 1 2 1 1 1 1
result:
ok 63 numbers
Test #4:
score: 0
Accepted
time: 1ms
memory: 9712kb
input:
120 5 1 1 1 1 1 5 1 1 1 1 2 5 1 1 1 1 3 5 1 1 1 1 4 5 1 1 1 1 5 5 1 1 1 2 1 5 1 1 1 2 2 5 1 1 1 2 3 5 1 1 1 2 4 5 1 1 1 2 5 5 1 1 1 3 1 5 1 1 1 3 2 5 1 1 1 3 3 5 1 1 1 3 4 5 1 1 1 3 5 5 1 1 1 4 1 5 1 1 1 4 2 5 1 1 1 4 3 5 1 1 1 4 4 5 1 1 1 4 5 5 1 1 2 1 1 5 1 1 2 1 2 5 1 1 2 1 3 5 1 1 2 1 4 5 1 1 2 ...
output:
1 2 2 2 2 -1 -1 -1 1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