QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#96026 | #6301. Minimum Suffix | psc233 | WA | 4ms | 11988kb | C++14 | 1.7kb | 2023-04-12 22:00:46 | 2023-04-12 22:00:50 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int MAXN=2e5+10;
template <typename _Tp>
inline _Tp read(_Tp &x) {
char ch = getchar(), sgn = 0; x = 0;
while (ch ^ '-' && !isdigit(ch)) ch = getchar();
if (ch == '-') ch = getchar(), sgn = 1;
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
if (sgn) x = -x;
return x;
}
const int N=3e6+10;
int p[N],a[N],b[N],ans[N],s[N];
int n,T,len,flag;
void solve(int l,int r){
int k=l;
if (p[l]!=l){
flag=0; return;
}
for (int i=l+1;i<=r;i++)
if (p[i]==l){
a[i]=k;
b[i]=1;
k=l;
}else{
if (p[i]<l) {flag=0;return;}
if ((i-p[i])!=(k-p[k])) {flag=0;return;}
a[i]=k;
b[i]=0;
k++;
}
a[l]=l; b[l]=1;
ans[l]=(len?s[1]:1);
bool OK=0;
for (int i=l+1;i<=r;i++){
if (i>len||OK){
if (b[i]) ans[i]=ans[a[i]]+1; else ans[i]=ans[a[i]];
}else{
if (b[i]){
ans[i]=ans[a[i]]+1;
if (ans[i]>s[i-l+1]) OK=1;
else if (ans[i]<s[i-l+1]){
ans[i]=s[i-l+1];
}
}else{
ans[i]=ans[a[i]];
if (ans[i]>s[i-l+1]) OK=1;
else if (ans[i]<s[i-l+1]){
for (int j=i-1;j>=1;j--) if (b[j]){
ans[j]++;
i=j; OK=1; break;
}
}
}
}
}
if ((r-l+1<len)&&!OK){
for (int j=r;j>=l;j--) if (b[j]){
ans[j]++;
for (int k=j+1;k<=r;k++) if (b[k]) ans[k]=ans[a[k]]+1; else ans[k]=ans[a[k]];
break;
}
}
for (int i=l;i<=r;i++) s[i-l+1]=ans[i];
len=r-l+1;
}
int main(){
scanf("%d",&T);
while (T--){
scanf("%d",&n);
flag=1;
for (int i=1;i<=n;i++) read(p[i]);
len=0;
int t=n;
while (t){
solve(p[t],t);
if (!flag) break;
t=p[t]-1;
}
if (flag){
for (int i=1;i<=n;i++) printf("%d ",ans[i]);
putchar('\n');
}else puts("-1");
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 11976kb
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: 2ms
memory: 11796kb
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: 11692kb
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: 4ms
memory: 11988kb
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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