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
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;
}

  

Nastya Is Buying Lunch的更多相关文章

  1. CF1136D Nastya Is Buying Lunch

    思路: 1. 最终答案不超过能与Nastya“直接交换”的人数. 2. 对于排在j前面的i,如果i和i-j之间(包括j)的每个人都能“直接交换”,j才能前进一步. 实现: #include <b ...

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

    这道题,神仙贪心题... 题意就是我给出数的顺序,并给出多个交换,每个只能用于相邻交换,问最后一个元素,最多能往前交换多少步. 我们考虑这样一个问题,如果一个这数和a[n]发生交换,那么这个数作为后面 ...

  3. cf1136D. Nastya Is Buying Lunch(贪心)

    题意 题目链接 给出一个排列,以及\(m\)个形如\((x, y)\)的限制,表示若\(x\)在\(y\)之前则可以交换\(x, y\). 问\(n\)位置上的数最多能前进几步 \(n \leqsla ...

  4. D. Nastya Is Buying Lunch

    链接 [https://codeforces.com/contest/1136/problem/D] 题意 有N个人,a[i]表示第i个人的编号,m个二元组. 当前一个在后一个的前面一个位置时二者可以 ...

  5. Codeforces 1136D - Nastya Is Buying Lunch - [贪心+链表+map]

    题目链接:https://codeforces.com/problemset/problem/1136/D 题意: 给出 $1 \sim n$ 的某个排列 $p$,再给出若干 $(x,y)$ 表示当序 ...

  6. Nastya Is Buying Lunch CodeForces - 1136D (排列)

    大意: 给定n排列, m个pair, 每个pair(u,v), 若u,v相邻, 且u在v左侧, 则可以交换u和v, 求a[n]最多向左移动多少 经过观察可以发现, 尽量先用右侧的人与a[n]交换, 这 ...

  7. Codeforces 1136D Nastya Is Buying Lunch (贪心)

    题意: 给一个序列和一组交换序列(a,b),当且仅当a在b的前面(不允许有间隔),这两个数才能交换,问最后一个数最多能移动多少个位置. 分析: 这题是思路是十分的巧妙呀 , 用一个数组num[x]  ...

  8. Codeforces 1136 - A/B/C/D/E - (Done)

    链接:https://codeforces.com/contest/1136/ A - Nastya Is Reading a Book - [二分] #include<bits/stdc++. ...

  9. Codeforces Round #546 (Div. 2) 题解

    Codeforces Round #546 (Div. 2) 题目链接:https://codeforces.com/contest/1136 A. Nastya Is Reading a Book ...

随机推荐

  1. python通过套接字来发送接收消息

    案例如下: 1.启动一个服务端套接字服务 2.启动一个客户端套接字服务 3.客户端向服务端发送一个hello,服务端则回复一个word,并打印 参考地址:https://www.cnblogs.com ...

  2. MySQL数据转移至SQL Server详解

    最近有个活是mysql数据转移到sql server 2012,直接手动转工作量太大,发现网上有工具教程,则记录一下. 一.安装MySQL ODBC驱动为MySQL安装Connector/ODBC驱动 ...

  3. DirectX11--深入理解与使用缓冲区资源

    前言 在Direct3D 11中,缓冲区属于其中一种资源类型,它在内存上的布局是一维线性的.根据HLSL支持的类型以及C++的使用情况,缓冲区可以分为下面这些类型: 顶点缓冲区(Vertex Buff ...

  4. 【转】Redis学习笔记(四)如何用Redis实现分布式锁(1)—— 单机版

    原文地址:http://bridgeforyou.cn/2018/09/01/Redis-Dsitributed-Lock-1/ 为什么要使用分布式锁 这个问题,可以分为两个问题来回答: 为什么要使用 ...

  5. Open vSwitch系列之二 安装指定版本ovs

    在ovs学习过程中,如果自己想要安装一个ovs交换机其实一条简单的命令 apt  install openvswitch 就可以了,但是这种方法只能安装低版本的ovs.在特殊情况下需要安装指定版本,例 ...

  6. seajs使用方法

    必须执行seajs.use()时,才能自动执行预加载项 <script src="/UILib/sea.js"></script> <script s ...

  7. vue api学习之nextTick的理解

    对于 Vue.nextTick 方法,之前没有听说过,突然听到别人提起,貌似作用挺大.以下为学习心得.官方文档上这样定义:在下次 DOM 更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法, ...

  8. JSON字符串与Map互转

    //一.map转为json字符串 public static String map2jsonstr(Map<String,?> map){ return JSONObject.toJSON ...

  9. mysql 8.0~MGR多成员读一致性

    一 背景:当在读节点多成员查询时可能导致数据不一致 二 三种场景   1 读多写少  AFTER    2 读写相当  AFTER_AND_BEFORE   3 读少写多  BEFORE三 数据不一致 ...

  10. 分布式系列十一: Redis进阶

    分布式锁 数据库 数据库是使用唯一索引不允许重复的特性(或自定义实现如乐观锁). 但持有锁的进程如果释放锁时异常则容易导致死锁. zookeeper 使用临时节点, watcher可以获得节点被删除的 ...