CF1023D.Array Restoration

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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)(l_i, r_i) (1≤li≤ri≤n)(1 \le l_i \le r_i \le n) was selected and values of elements on positions from lil_i to rir_i 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.

最初有一个由 nn 个整数组成的数组 aa,其位置编号为 11 到 nn。

对该数组恰好执行了 qq 次查询。在第 ii 次查询中,选择某个区间 (li,ri)(l_i, r_i)(满足 1≤li≤ri≤n1 \le l_i \le r_i \le n),并将位置 lil_i 到 rir_i(含端点)上所有元素的值修改为 ii。查询的顺序不可更改,且全部 qq 次查询均被依次应用。此外,已知从位置 11 到 nn 的每一个位置至少被某个区间覆盖过一次。

我们本可以向你提出这样一个问题:判断某个给定的数组(由 nn 个取值范围为 11 到 qq 的整数组成)是否能通过上述查询过程得到。然而,我们认为这个问题对你而言过于简单。

因此,我们对此问题做了如下增强:该数组中某一部分位置(可能为空)被选中,并将这些位置上的元素值设为 00。

你的任务是判断该数组是否能通过上述查询过程得到;若可以,则恢复出这个原始数组。

如果存在多个可能的原始数组,输出任意一个即可。

输入格式

The first line contains two integers nn and qq (1≤n,q≤2⋅1051 \le n, q \le 2 \cdot 10^5) — the number of elements of the array and the number of queries perfomed on it.

The second line contains nn integer numbers a1,a2,…,ana_1, a_2, \dots, a_n (0≤ai≤q0 \le a_i \le q) — the resulting array. If element at some position jj is equal to 00 then the value of element at this position can be any integer from 11 to qq.

第一行包含两个整数 nn 和 qq(1≤n,q≤2⋅1051 \le n, q \le 2 \cdot 10^5)—— 分别表示数组的元素个数以及在其上执行的查询次数。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(0≤ai≤q0 \le a_i \le q)—— 表示最终得到的数组。若某个位置 jj 上的元素值为 00,则该位置上的元素值可以是 11 到 qq 中的任意整数。

输出格式

Print "YES" if the array aa can be obtained by performing qq queries. Segments (li,ri)(l_i, r_i) (1≤li≤ri≤n)(1 \le l_i \le r_i \le 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.

如果数组 aa 可以通过执行 qq 次查询得到,则输出 "YES"。每次查询独立地选择一个区间 (li,ri)(l_i, r_i)(其中 1≤li≤ri≤n1 \le l_i \le r_i \le n)。位置 11 到 nn 中的每一个都必须被至少一个区间覆盖。

否则输出 "NO"。

若存在某个数组可被如此获得,则在第二行输出 nn 个整数——第 ii 个数应等于结果数组的第 ii 个元素,且其值应在 11 到 qq 之间。该数组必须能通过恰好执行 qq 次查询得到。

若存在多个可能的数组,输出任意一个即可。

输入输出样例

  • 输入#1

    4 3
    1 0 2 3

    输出#1

    YES
    1 2 2 3
  • 输入#2

    3 10
    10 10 10

    输出#2

    YES
    10 10 10
  • 输入#3

    5 6
    6 5 6 2 2

    输出#3

    NO
  • 输入#4

    3 5
    0 0 0

    输出#4

    YES
    5 4 2

说明/提示

In the first example you can also replace 00 with 11 but not with 33.

In the second example it doesn't really matter what segments to choose until query 1010 when the segment is (1,3)(1, 3).

The third example showcases the fact that the order of queries can't be changed, you can't firstly set (1,3)(1, 3) to 66 and after that change (2,2)(2, 2) to 55. The segment of 55 should be applied before segment of 66.

There is a lot of correct resulting arrays for the fourth example.

在第一个例子中,你也可以将 00 替换为 11,但不能替换为 33。

在第二个例子中,在查询 1010(此时区间为 (1,3)(1, 3))之前,选择哪些区间实际上并不重要。

第三个例子说明了查询顺序不可更改:你不能先将区间 (1,3)(1, 3) 设为 66,再将区间 (2,2)(2, 2) 改为 55;值为 55 的区间的操作必须在值为 66 的区间的操作之前执行。

第四个例子存在大量合法的最终数组。

输入解题思路,AI测评打分。不知道怎么写?

首页