【Codeforces Round #546(Div. 2)】Nastya Is Buying Lunch(贪心)

对不起最近又变菜了....我反思...我忏悔

虽然这道题有点贪心的感觉,但是还是没想出来qwq最后搜了题解

题目链接

D. Nastya Is Buying Lunch

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 (1≤n≤3⋅1051≤n≤3⋅105, 0≤m≤5⋅1050≤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 (1≤pi≤n1≤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 (1≤ui,vi≤n,ui≠vi1≤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 i≠ji≠j, than vi≠vjvi≠vj or ui≠ujui≠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

Copy

2 1
1 2
1 2

output

Copy

1

input

Copy

3 3
3 1 2
1 2
3 1
3 2

output

Copy

2

input

Copy

5 2
3 1 5 4 2
5 2
5 4

output

Copy

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.

【题意】

给一个序列和一组交换序列(a,b),当且仅当a在b的前面(不允许有间隔),这两个数才能交换,问最后一个数最多能移动多少个位置。

【解题思路】

为了让最后一个数往前移,肯定需要前面的数和它交换。因为只有相邻的数可以交换,所以首先想到第n-1个位置的数是否能够和它交换呢?如果不可以的话,那么在前面有那个数可以和第n-1个位置交换呢?嗯……这是我的最初想法,就这么一直递推回去,非常…朴实。

但事实上做法却很巧妙,用数组num[x]记录数x后面可以与其交换的数的数目,当这个数目刚好等于这个数和最后一个数的距离时,肯定有办法能把最后一个数换到x的位置,然后需要注意的是要一直更新最后一个数的位置,它的位置就是n-ans(即数列长度-已经移动的次数)。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+5;
vector<int>v[maxn];
int num[maxn],a[maxn];
int main()
{
    int n,m,ans=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    while(m--)
    {
        int uu,vv;
        scanf("%d%d",&uu,&vv);
        v[vv].push_back(uu);
    }
    for(int i=0;i<v[a[n]].size();i++)
        num[v[a[n]][i]]++;
    for(int i=n-1;i>=1;i--)
    {
        if(num[a[i]]==n-i-ans)ans++;
        else
        {
            for(int j=0;j<v[a[i]].size();j++)
                num[v[a[i]][j]]++;
        }
    }
    printf("%d\n",ans);
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/88534762