Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)
http://codeforces.com/contest/1023/problem/D
D. Array Restoration
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Initially there was an array a
consisting of n integers. Positions in it are numbered from 1 to n
.
Exactly q
queries were performed on the array. During the i-th query some segment (li,ri) (1≤li≤ri≤n) was selected and values of elements on positions from li to ri inclusive got changed to i. The order of the queries couldn't be changed and all q queries were applied. It is also known that every position from 1 to n
got covered by at least one segment.
We could have offered you the problem about checking if some given array (consisting of n
integers with values from 1 to q
) can be obtained by the aforementioned queries. However, we decided that it will come too easy for you.
So the enhancement we introduced to it is the following. Some set of positions (possibly empty) in this array is selected and values of elements on these positions are set to 0
.
Your task is to check if this array can be obtained by the aforementioned queries. Also if it can be obtained then restore this array.
If there are multiple possible arrays then print any of them.
Input
The first line contains two integers n
and q (1≤n,q≤2⋅105
) — the number of elements of the array and the number of queries perfomed on it.
The second line contains n
integer numbers a1,a2,…,an (0≤ai≤q) — the resulting array. If element at some position j is equal to 0 then the value of element at this position can be any integer from 1 to q
.
Output
Print "YES" if the array a
can be obtained by performing q queries. Segments (li,ri) (1≤li≤ri≤n) are chosen separately for each query. Every position from 1 to n
should be covered by at least one segment.
Otherwise print "NO".
If some array can be obtained then print n
integers on the second line — the i-th number should be equal to the i-th element of the resulting array and should have value from 1 to q. This array should be obtainable by performing exactly q
queries.
If there are multiple possible arrays then print any of them.
Examples
Input
Copy
4 3 1 0 2 3
Output
Copy
YES 1 2 2 3
Input
Copy
3 10 10 10 10
Output
Copy
YES 10 10 10
Input
Copy
5 6 6 5 6 2 2
Output
Copy
NO
Input
Copy
3 5 0 0 0
Output
Copy
YES 5 4 2
Note
In the first example you can also replace 0
with 1 but not with 3
.
In the second example it doesn't really matter what segments to choose until query 10
when the segment is (1,3)
.
The third example showcases the fact that the order of queries can't be changed, you can't firstly set (1,3)
to 6 and after that change (2,2) to 5. The segment of 5 should be applied before segment of 6
.
There is a lot of correct resulting arrays for the fourth example.
题意:
这道题的意思就是给一个n个初始数字的序列。
问能不能通过一系列操作将0转化后,变成合法的串。一系列操作就是第i次可以将某段闭区间都变成i。一共要操作q次(必须操作q次)
思路:1.该串必须要有最大的数字q。否则就是不合法的。如果原串没有0,并且没有最大值,说明就是不合法的(坑点).
2.原串两个相同的数字之间所有的数字都不能小于它,因为小于它的先刷,而它是后来刷的(连续一段).这种情况我用的树状数组巧妙判断。
3.剩下的0的位置,如果没有q就找一个填成q,剩下的都跟前边的一样就好了。这样一定是可以的。
4.LHJ大佬告诉我的思路。
代码:
#include
using namespace std;
const int maxn=200000+100;
typedef long long LL;
int a[maxn];
int C[maxn];
int vis[maxn];
int n,q;
void add(int i,int x){
for(;i0){
he+=C[i];
i-=(i&(-i));
}
return he;
}
int main()
{
int flag;
scanf("%d%d",&n,&q);
memset(vis,0,sizeof(vis));flag=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++){
if(!a[i])
continue;
if(vis[a[i]]){///之前出现过了
if(vis[a[i]]!=sum(a[i])){///说明两个相等的中间有比它小的。不合法
flag=1;
break;
}
add(a[i],1);
vis[a[i]]=sum(a[i]);
}
else{
add(a[i],1);
vis[a[i]]=sum(a[i]);///到当前位置比a[i]小的有几个
}
}
if(flag)
{
printf("NO\n");
return 0;
}
else{
///cout<<"&&&"<