QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#510142 | #6301. Minimum Suffix | yyyyxh | WA | 0ms | 9692kb | C++14 | 1.4kb | 2024-08-08 21:29:03 | 2024-08-08 21:29:03 |
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;
if(eq[t]) cmp=0;
while(true){
a[t]=a[y];
if(t>stk[i]&&eq[t]) t=las[t];
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];
}
}
printf("%d\n",eq[2]);
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: 0
Wrong Answer
time: 0ms
memory: 9692kb
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:
0 1 2 2 -1 0 1 2 1 1 1 1 2 1 2 1 2 1 1 1 1
result:
wrong answer 1st numbers differ - expected: '1', found: '0'