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 n pupils in the school, numbered from 1 to n. 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 u, v such that if the pupil with number u stands directly in front of the pupil with number v, 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.
课间休息时,娜斯佳来到了学校食堂。学校共有 n 名学生,编号从 1 到 n。不幸的是,娜斯佳来得较晚,此时所有学生已排好队,即娜斯佳排在队伍的最后一位。这固然让娜斯佳略感沮丧,但她并不气馁,因为队列中某些学生愿意与其他学生交换位置。
形式化地说:存在若干对 (u,v),表示若编号为 u 的学生恰好站在编号为 v 的学生正前方,则娜斯佳可以请求他们,而他们会互换位置。
娜斯佳请你求出她最多能在队列中向前移动多少个位置。
输入格式
The first line contains two integers n and m (1≤n≤3⋅105, 0≤m≤5⋅105) — 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 n integers p1, p2, ..., pn — the initial arrangement of pupils in the queue, from the queue start to its end (1≤pi≤n, p is a permutation of integers from 1 to n). In other words, pi is the number of the pupil who stands on the i-th position in the queue.
The i-th of the following m lines contains two integers ui, vi (1≤ui,vi≤n,ui=vi), denoting that the pupil with number ui agrees to change places with the pupil with number vi if ui is directly in front of vi. It is guaranteed that if i=j, than vi=vj or ui=uj. 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 pn.
第一行包含两个整数 n 和 m(1≤n≤3⋅105,0≤m≤5⋅105)—— 分别表示队列中学生的数量,以及满足“前者愿意与后者交换位置(当且仅当前者恰好站在后者正前方)”这一条件的学生对的数量。
第二行包含 n 个整数 p1, p2, ..., pn —— 表示队列的初始排列,从队首到队尾(1≤pi≤n,且 p 是 1 到 n 的一个排列)。换言之,pi 表示站在队列第 i 个位置上的学生的编号。
接下来的 m 行中,第 i 行包含两个整数 ui, vi(1≤ui,vi≤n,ui=vi),表示编号为 ui 的学生愿意与编号为 vi 的学生交换位置(当且仅当 ui 恰好站在 vi 正前方)。保证当 i=j 时,(ui,vi)=(uj,vj)。注意:某些学生对可能互相都愿意与对方交换位置。
纳斯塔亚(Nastya)位于队列末尾,即编号为 pn 的学生。
输出格式
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 1 and 3.
- change places for pupils with numbers 3 and 2.
- change places for pupils with numbers 1 and 2.
The queue looks like [3,1,2], then [1,3,2], then [1,2,3], and finally [2,1,3] after these operations.
在第一个例子中,Nastya 只需与队列中的第一位同学交换位置即可。
第二个例子中最优的交换序列为:
- 交换编号为 1 和 3 的同学的位置。
- 交换编号为 3 和 2 的同学的位置。
- 交换编号为 1 和 2 的同学的位置。
经过这些操作后,队列依次变为 [3,1,2]、[1,3,2]、[1,2,3],最终为 [2,1,3]。
输入解题思路,AI测评打分。不知道怎么写?