Codeforces Round #546 (Div. 2) 1136D. Nastya Is Buying Lunch

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.

 

题意:给一个1到N的随机排列,以及m对数(u,v)当且仅当u在v前面一个的时候,可以交换它们的位置,问最多能将最后一个数前移多少位。

 

分析:朴素的想法,想要将a[i]前移,首先看能否和a[i-1]交换,若不行,再看a[i-2]与a[i-1],若a[i-2]能同时与a[i-1]和a[i]交换,那么能将a[i-2]移到a[i]后面,a[i-1]和a[i]整体前移一位。

考虑普遍的情况,对于i到j,如果a[i]到a[j],都有前缀a[i-1],那么就可以将a[i-1]移到最后一位,将a[i]到a[j]整体前移一位。

然而暴力求交集是不可行的,我们可以从v到u建单向边,用一个num[x]数组,表示x后面与x有边的点的数量,因为位移是整体前移,所以每个点对前面的贡献不会消失。

当num[x]=x到i的距离时,可以将x移到i后面,并且x不对前面的点有贡献。

整体复杂度O(n+m)

#include <bits/stdc++.h>
using namespace std;
int a[300004];
int num[300004];
vector<int>v[300004];
int main (){
    int n,m;
    cin>>n>>m;
    for (int i = 1; i <= n; ++i) {
        scanf("%d",&a[i]);
    }
    int x,y;
    while(m--)
    {
        scanf("%d%d",&x,&y);
        v[y].push_back(x);
    }
    memset(num,0, sizeof(num));
    for (int i = 0; i < v[a[n]].size(); ++i) {
        num[v[a[n]][i]]++;
    }
    int ans=0;
    for (int i = n-1; i >=1 ; --i) {
        if(num[a[i]]==n-ans-i)ans++;
        else
        {
            for (int j = 0; j < v[a[i]].size(); ++j) {
                num[v[a[i]][j]]++;
            }
        }
    }
    printf("%d\n",ans);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值