Codeforces 1136D 【思维】

D. Nastya Is Buying Lunch

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.

Input

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.

Output

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

Examples

input
2 1
1 2
1 2
output
1

input
3 3
3 1 2
1 2
3 1
3 2
output
2

input
5 2
3 1 5 4 2
5 2
5 4
output
1

Note

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.

Solution

有n个人在打饭,给出一行的人物编号和愿意交换位置的两个人编号(必须相邻才能交换),求问最后一个人可以往前换多少位。从后往前逐个找,记录每个位置可被后边的调换位置的个数,如果个数加上原位置与最后一个位置已到达的位置n-ans-1(-1是从0开始计算)相等,说明可调换,计数ans++

Code

#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 3e5+5;
typedef long long ll;
#define pb push_back

int n,m,l,r,a[maxn],cnt[maxn];
vector<int> v[maxn];

int main() {
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);  //不加TLE
	cin >> n >> m;
	for(int i=0;i<n;i++) {
		cin >> a[i];
	}
	for(int i=0;i<m;i++) {
		cin >> l >> r;
		v[r].pb(l);
	}
	for(int i=0;i<v[a[n-1]].size();i++) {
		cnt[v[a[n-1]][i]]++;
	}
	int ans = 0;
	for(int i=n-2;i>=0;i--){
        if(i+cnt[a[i]]==n-1-ans) {
            ans++;
		}
        else{
            for(int j=0;j<v[a[i]].size();j++) {
                cnt[v[a[i]][j]]++;
			}
        }
    }
    cout << ans << endl;
}

Source

Codeforces 1136D Nastya Is Buying Lunch

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值