代码
#include<bits/stdc++.h>
using namespace std;
int T;
int n;
int L,R;
int cnt,len;
bool flag;
struct node
{
int v;
int p;
bool operator < (const node x) const
{
return v<x.v;
}
};
node a[100005];
int b[100005];
int main()
{
cin>>T;
for(int t=1;t<=T;t++)
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].v;
a[i].p=i;
}
sort(a+1,a+n+1);
L=1,R=n;
cnt=0;
len=n;
flag=true;
while(L<=R)
{
int l=a[L].v-cnt;
int r=a[R].v-cnt;
if(l==0&&r==len||l!=0&&r!=len)
{
flag = false;
break;
}
if(l==0)
{
b[a[L].p]=-len;
L++;
len--;
}
else if(r==len)
{
b[a[R].p] = len;
R--;
cnt++;
len--;
}
}
if(flag==false) cout<<"NO"<<endl;
else
{
cout<<"YES"<<endl;
for(int i=1;i<=n;i++)
{
cout<<b[i]<<" ";
}
cout<<endl;
}
}
return 0;
}
题面
Input 1
5
1
1
4
1 4 3 4
3
0 1 0
4
4 3 2 1
3
1 3 1
Output 1
YES
1
NO
YES
-3 1 -2
YES
4 2 -1 -3
YES
-1 3 -1
My output
YES
1
NO
YES
-3 1 -2
YES
4 2 -1 -3
YES
-2 3 -1
最后一组是输出
-1 3 -1
,我输出了-2 3 -1