c语言sscanf函数的用法是什么
278
2022-08-30
D. Array Restoration(树状数组+分类)
Initially there was an array aa consisting of nn integers. Positions in it are numbered from 11 to nn.
Exactly qq queries were performed on the array. During the ii-th query some segment (li,ri)(li,ri) (1≤li≤ri≤n)(1≤li≤ri≤n) was selected and values of elements on positions from lili to riri inclusive got changed to ii. The order of the queries couldn't be changed and all qq queries were applied. It is also known that every position from 11 to nn got covered by at least one segment.
We could have offered you the problem about checking if some given array (consisting of nn integers with values from 11 to qq) 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 00.
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 nn and qq (1≤n,q≤2⋅1051≤n,q≤2⋅105) — the number of elements of the array and the number of queries perfomed on it.
The second line contains nn integer numbers a1,a2,…,ana1,a2,…,an (0≤ai≤q0≤ai≤q) — the resulting array. If element at some position jj is equal to 00then the value of element at this position can be any integer from 11 to qq.
Output
Print "YES" if the array aa can be obtained by performing qq queries. Segments (li,ri)(li,ri) (1≤li≤ri≤n)(1≤li≤ri≤n) are chosen separately for each query. Every position from 11 to nn should be covered by at least one segment.
Otherwise print "NO".
If some array can be obtained then print nn integers on the second line — the ii-th number should be equal to the ii-th element of the resulting array and should have value from 11 to qq. This array should be obtainable by performing exactly qq queries.
If there are multiple possible arrays then print any of them.
Examples
input
4 3 1 0 2 3
output
YES 1 2 2 3
input
3 10 10 10 10
output
YES 10 10 10
input
5 6 6 5 6 2 2
output
NO
input
3 5 0 0 0
output
YES 5 4 2
题目大概:
给你一个数组,长度为n,0表示空白位,可以随便填。然后问你原数组是否能够进行q次操作,第 qi 次操作把一个区间中所有数变成 i 。能输出YES,并输出任意一种数组。否则NO。
思路:
1 最一般的情况是只要出现,两个相同的数中间,出现比它小的数,就不合法。这种情况我是用的树状数组维护的。
2 还有如果没有最大的q,如果有0,需要添加最大的q,否则输出NO。
3 如果还有0,直接填充成它前面的数,这样一定不会错。(这里注意把数组的0位置设置成合法的数)
代码:
#include
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~