CF1765H.Hospital Queue

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

There are nn people (numbered from 11 to nn) signed up for a doctor's appointment. The doctor has to choose in which order he will appoint these people. The ii-th patient should be appointed among the first pip_i people. There are also mm restrictions of the following format: the ii-th restriction is denoted by two integers (ai,bi)(a_i, b_i) and means that the patient with the index aia_i should be appointed earlier than the patient with the index bib_i.

For example, if n=4n = 4, p=[2,3,2,4]p = [2, 3, 2, 4], m=1m = 1, a=[3]a = [3] and b=[1]b = [1], then the only order of appointment of patients that does not violate the restrictions is [3,1,2,4][3, 1, 2, 4]. For n=3n =3, p=[3,3,3]p = [3, 3, 3], m=0m = 0, a=[]a = [] and b=[]b = [], any order of appointment is valid.

For each patient, calculate the minimum position in the order that they can have among all possible orderings that don't violate the restrictions.

有 nn 个人(编号从 11 到 nn)预约了医生的就诊。医生需要确定这些人就诊的顺序。第 ii 位患者必须安排在前 pip_i 个就诊者之中。此外还有 mm 条限制,每条限制的格式为两个整数 (ai,bi)(a_i, b_i),表示编号为 aia_i 的患者必须比编号为 bib_i 的患者更早就诊。

例如,若 n=4n = 4,p=[2,3,2,4]p = [2, 3, 2, 4],m=1m = 1,a=[3]a = [3],b=[1]b = [1],则唯一不违反限制的就诊顺序是 [3,1,2,4][3, 1, 2, 4]。又如,若 n=3n = 3,p=[3,3,3]p = [3, 3, 3],m=0m = 0,a=[]a = [],b=[]b = [],则任意就诊顺序均合法。

对每位患者,请计算其在所有不违反限制的就诊顺序中可能占据的最小位置(即最靠前的位置)。

输入格式

The first line contains two integers nn and mm (1≤n≤20001 \le n \le 2000; 0≤m≤20000 \le m \le 2000).

The second line contains nn integers p1,p2,…,pnp_1, p_2, \dots, p_n (1≤pi≤n1 \le p_i \le n).

Then mm lines follow. The ii-th of them contains two integers aia_i and bib_i (1≤ai,bi≤n1 \le a_i, b_i \le n; ai≠bia_i \ne b_i). All pairs of (ai,bi)(a_i, b_i) are distinct (i. e. if i≠ji \ne j, then either ai≠aja_i \ne a_j, bi≠bjb_i \ne b_j, or both).

Additional constraint on the input: there is at least one valid order of patients.

第一行包含两个整数 nn 和 mm(1≤n≤20001 \le n \le 2000;0≤m≤20000 \le m \le 2000)。

第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \dots, p_n(1≤pi≤n1 \le p_i \le n)。

接下来是 mm 行。其中第 ii 行包含两个整数 aia_i 和 bib_i(1≤ai,bi≤n1 \le a_i, b_i \le n;ai≠bia_i \ne b_i)。所有 (ai,bi)(a_i, b_i) 对互不相同(即若 i≠ji \ne j,则 ai≠aja_i \ne a_j、bi≠bjb_i \ne b_j 或两者同时成立)。

输入的附加约束:至少存在一种合法的患者就诊顺序。

输出格式

Print nn integers, where ii-th integer is equal to the minimum position of ii-th patient in the order, among all valid orders. Positions in the order are numbered from 11 to nn.

输出 nn 个整数,其中第 ii 个整数等于在所有合法顺序中,第 ii 位患者所在位置的最小值。顺序中的位置编号从 11 到 nn。

输入输出样例

  • 输入#1

    4 1
    2 3 2 4
    3 1

    输出#1

    2 3 1 4
  • 输入#2

    3 0
    3 3 3

    输出#2

    1 1 1
  • 输入#3

    5 3
    4 3 3 2 5
    3 1
    1 5
    4 2

    输出#3

    4 2 1 1 5

说明/提示

In the first example, [3,1,2,4][3, 1, 2, 4] the only one valid order, so the minimum position of each patient is equal to their position in this order.

In the second example, any order is valid, so any patient can be appointed first.

In the third example, there are three valid orders: [4,2,3,1,5][4, 2, 3, 1, 5], [3,4,2,1,5][3, 4, 2, 1, 5] and [4,3,2,1,5][4, 3, 2, 1, 5].

在第一个例子中,[3,1,2,4][3, 1, 2, 4] 是唯一有效的顺序,因此每位患者的最小位置即为该顺序中对应的位置。

在第二个例子中,任意顺序均有效,因此任何患者均可被安排在第一位。

在第三个例子中,存在三种有效的顺序:[4,2,3,1,5][4, 2, 3, 1, 5]、[3,4,2,1,5][3, 4, 2, 1, 5] 和 [4,3,2,1,5][4, 3, 2, 1, 5]。

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

首页