Nastya Is Buying Lunch

本文介绍了一个关于学生在食堂排队的问题,通过一系列换位操作,计算Nastya能向前移动的最大位置数。涉及到的数据结构包括向量和数组,通过算法解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Input

The first line contains two integers nn and mm (1n31051≤n≤3⋅105, 0m51050≤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 nn integers p1p1, p2p2, ..., pnpn — the initial arrangement of pupils in the queue, from the queue start to its end (1pin1≤pi≤n, pp is a permutation of integers from 11 to nn). In other words, pipi 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 uiui, vivi (1ui,vin,uivi1≤ui,vi≤n,ui≠vi), denoting that the pupil with number uiui agrees to change places with the pupil with number vivi if uiui is directly in front of vivi. It is guaranteed that if iji≠j, than vivjvi≠vj or uiujui≠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 pnpn.

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 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.

 

 

#include<vector>
#include<iostream>
#include<cstdio>
#include<algorithm>
const int N = 3e5+10;
using namespace std;
vector<int >v[N];
int num[N],pos[N],cnt[N];
int main()
{
	int i,n,m,ans = 0,x,a,b;
	scanf("%d%d",&n,&m);
	for(i = 1; i <= n; i++){
		scanf("%d",&x);
		num[i] = x;
		pos[x] = i;
	}
	for(i = 0; i < m; i++){
		scanf("%d%d",&a,&b);
		if(pos[b] > pos[a]){
			v[b].push_back(a);
		}
	}
	for(i = 0; i<v[num[n]].size(); i++){
		cnt[v[num[n]][i]]++;
	}
	for(i = n-1; i > 0; i--){
		if(cnt[num[i]] == n-i-ans){
			ans++;
		}
		else{
			for(int j = 0; j < v[num[i]].size(); i++){
				cnt[v[num[i]][j]]++;
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}

  

转载于:https://www.cnblogs.com/clb123/p/10596599.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值