题目大意:
定义一个符号
i≺j
表示序列中
i
在
现在有一个由
1,2,.......,N,1′,2′,........,N′
满足
i≺j⇔i ′≺j ′
。现在定义
bij={j ′,if j<ij ,if j>i
,然后有
ai=∑Nj=1[i≺bij]
(
bij
无意义)。
现在给你所有的
ai
,如果存在一个数列满足上述条件,那么输出
“ YES ”
,并且输出这个数列,否则输出
“NO”
。
解题思路:
我们考虑贪心构造,每次优先将当前
ai=0
的最小的
i
,并将
AC代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
int N;
int a[1010]={0};
int hash[1010]={0};
int ans[2020]={0};
int dui[1010]={0};
int st=1,en=0;
int main()
{
scanf("%d",&N);
for(int i=1;i<=N;i++)
scanf("%d",&a[i]);
a[0]=2e9;
for(int p=1;p<=N*2;p++)
{
int i;
for(i=1;i<=N;i++)
if(a[i]==0 && hash[i]==0)
break;
if(i<=N)
{
ans[p]=i;
for(int j=1;j<i;j++)
if(a[j]>0)
a[j]--;
dui[++en]=i;
hash[i]=1;
}
else
{
if(st>en)
{
puts("NO");
return 0;
}
ans[p]=-dui[st];
for(int j=dui[st]+1;j<=N;j++)
if(a[j]>0)
a[j]--;
st++;
}
}
puts("YES");
for(int i=N*2;i>=1;i--)
printf("%d ",ans[i]);
return 0;
}