CF1136D.Nastya Is Buying Lunch

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

At the big break Nastya came to the school dining room. There are nn pupils in the school, numbered from 11 to nn. Unfortunately, Nastya came pretty late, so that all pupils had already stood in the queue, i.e. Nastya took the last place in the queue. Of course, it's a little bit sad for Nastya, but she is not going to despond because some pupils in the queue can agree to change places with some other pupils.

Formally, there are some pairs uu, vv such that if the pupil with number uu stands directly in front of the pupil with number vv, Nastya can ask them and they will change places.

Nastya asks you to find the maximal number of places in queue she can move forward.

课间休息时,娜斯佳来到了学校食堂。学校共有 nn 名学生,编号从 11 到 nn。不幸的是,娜斯佳来得较晚,此时所有学生已排好队,即娜斯佳排在队伍的最后一位。这固然让娜斯佳略感沮丧,但她并不气馁,因为队列中某些学生愿意与其他学生交换位置。

形式化地说:存在若干对 (u,v)(u, v),表示若编号为 uu 的学生恰好站在编号为 vv 的学生正前方,则娜斯佳可以请求他们,而他们会互换位置。

娜斯佳请你求出她最多能在队列中向前移动多少个位置。

输入格式

The first line contains two integers nn and mm (1≤n≤3⋅1051 \leq n \leq 3 \cdot 10^{5}, 0≤m≤5⋅1050 \leq m \leq 5 \cdot 10^{5}) — the number of pupils in the queue and number of pairs of pupils such that the first one agrees to change places with the second one if the first is directly in front of the second.

The second line contains nn integers p1p_1, p2p_2, ..., pnp_n — the initial arrangement of pupils in the queue, from the queue start to its end (1≤pi≤n1 \leq p_i \leq n, pp is a permutation of integers from 11 to nn). In other words, pip_i is the number of the pupil who stands on the ii-th position in the queue.

The ii-th of the following mm lines contains two integers uiu_i, viv_i (1≤ui,vi≤n,ui≠vi1 \leq u_i, v_i \leq n, u_i \neq v_i), denoting that the pupil with number uiu_i agrees to change places with the pupil with number viv_i if uiu_i is directly in front of viv_i. It is guaranteed that if i≠ji \neq j, than vi≠vjv_i \neq v_j or ui≠uju_i \neq u_j. Note that it is possible that in some pairs both pupils agree to change places with each other.

Nastya is the last person in the queue, i.e. the pupil with number pnp_n.

第一行包含两个整数 nn 和 mm(1≤n≤3⋅1051 \leq n \leq 3 \cdot 10^{5},0≤m≤5⋅1050 \leq m \leq 5 \cdot 10^{5})—— 分别表示队列中学生的数量,以及满足“前者愿意与后者交换位置(当且仅当前者恰好站在后者正前方)”这一条件的学生对的数量。

第二行包含 nn 个整数 p1p_1, p2p_2, ..., pnp_n —— 表示队列的初始排列,从队首到队尾(1≤pi≤n1 \leq p_i \leq n,且 pp 是 11 到 nn 的一个排列)。换言之,pip_i 表示站在队列第 ii 个位置上的学生的编号。

接下来的 mm 行中,第 ii 行包含两个整数 uiu_i, viv_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n,ui≠viu_i \neq v_i),表示编号为 uiu_i 的学生愿意与编号为 viv_i 的学生交换位置(当且仅当 uiu_i 恰好站在 viv_i 正前方)。保证当 i≠ji \neq j 时,(ui,vi)≠(uj,vj)(u_i, v_i) \neq (u_j, v_j)。注意:某些学生对可能互相都愿意与对方交换位置。

纳斯塔亚(Nastya)位于队列末尾,即编号为 pnp_n 的学生。

输出格式

Print a single integer — the number of places in queue she can move forward.

输出一个整数——她可以在队列中向前移动的位置数。

输入输出样例

  • 输入#1

    2 1
    1 2
    1 2

    输出#1

    1
  • 输入#2

    3 3
    3 1 2
    1 2
    3 1
    3 2

    输出#2

    2
  • 输入#3

    5 2
    3 1 5 4 2
    5 2
    5 4

    输出#3

    1

说明/提示

In the first example Nastya can just change places with the first pupil in the queue.

Optimal sequence of changes in the second example is

  • change places for pupils with numbers 11 and 33.
  • change places for pupils with numbers 33 and 22.
  • change places for pupils with numbers 11 and 22.

The queue looks like [3,1,2][3, 1, 2], then [1,3,2][1, 3, 2], then [1,2,3][1, 2, 3], and finally [2,1,3][2, 1, 3] after these operations.

在第一个例子中,Nastya 只需与队列中的第一位同学交换位置即可。

第二个例子中最优的交换序列为:

  • 交换编号为 11 和 33 的同学的位置。
  • 交换编号为 33 和 22 的同学的位置。
  • 交换编号为 11 和 22 的同学的位置。

经过这些操作后,队列依次变为 [3,1,2][3, 1, 2]、[1,3,2][1, 3, 2]、[1,2,3][1, 2, 3],最终为 [2,1,3][2, 1, 3]。

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

首页