C. Candies Distribution
There are nn children numbered from 11 to nn in a kindergarten. Kindergarten teacher gave aiai (1≤ai≤n1≤ai≤n) candies to the ii-th child. Children were seated in a row in order from 11 to nn from left to right and started eating candies.
While the ii-th child was eating candies, he calculated two numbers lili and riri — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.
Formally, lili is the number of indices jj (1≤j<i1≤j<i), such that ai<ajai<aj and riri is the number of indices jj (i<j≤ni<j≤n), such that ai<ajai<aj.
Each child told to the kindergarten teacher the numbers lili and riri that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays ll and rr determine whether she could have given the candies to the children such that all children correctly calculated their values lili and riri, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.
Input
On the first line there is a single integer nn (1≤n≤10001≤n≤1000) — the number of children in the kindergarten.
On the next line there are nn integers l1,l2,…,lnl1,l2,…,ln (0≤li≤n0≤li≤n), separated by spaces.
On the next line, there are nn integer numbers r1,r2,…,rnr1,r2,…,rn (0≤ri≤n0≤ri≤n), separated by spaces.
Output
If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes).
Otherwise, print «YES» (without quotes) on the first line. On the next line, print nn integers a1,a2,…,ana1,a2,…,an, separated by spaces — the numbers of candies the children 1,2,…,n1,2,…,n received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition 1≤ai≤n1≤ai≤n. The number of children seating to the left of the ii-th child that got more candies than he should be equal to lili and the number of children seating to the right of the ii-th child that got more candies than he should be equal to riri. If there is more than one solution, find any of them.
Examples
input
Copy
5
0 0 1 1 2
2 0 1 0 0
output
Copy
YES
1 3 1 2 1
input
Copy
4
0 0 2 0
1 1 1 1
output
Copy
NO
input
Copy
3
0 0 0
0 0 0
output
Copy
YES
1 1 1
题意:现在有个数组,对于第i个数,它左边比它大的个数li,右边比它大的个数ri,现在给你li和ri数组,求出当前数组。
思路:li和ri合起来就是:比第i个数大的总个数,题目要求ai<=n,既然有li+ri个数比ai大,考虑最差情况,li+ri个数都不相同,可得最大的ai:n-li-ri,以此类推,生成数组,此时每个ai都是满足li和ri条件下的最大可取ai(只能这么表达了- -)。
也就意味着当前ai数组不能与li和ri出现冲突的,最后检查一下是否有冲突即可。
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n,a[1010],b[1010],c[1010],mmax,flag;
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)scanf("%d",&b[i]);
for(int i=1;i<=n;i++)c[i]=n-a[i]-b[i];
flag=0;
for(int i=1;i<=n;i++) //检查li是否合理
{
mmax=0;
for(int j=1;j<i;j++)
{
if(c[j]>c[i])mmax++;
}
if(mmax!=a[i])
{
flag=1;
break;
}
}
for(int i=1;i<=n;i++) //检查ri是否合理
{
mmax=0;
for(int j=n;j>i;j--)
{
if(c[j]>c[i])mmax++;
}
if(mmax!=b[i])
{
flag=1;
break;
}
}
if(flag==1)printf("NO\n");
else
{
printf("YES\n");
for(int i=1;i<=n;i++)
{
printf("%d ",c[i]);
}
printf("\n");
}
}
return 0;
}