Nastya Is Buying Lunch
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
2 1
1 2
1 2
1
3 3
3 1 2
1 2
3 1
3 2
2
5 2
3 1 5 4 2
5 2
5 4
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的更多相关文章
- CF1136D Nastya Is Buying Lunch
思路: 1. 最终答案不超过能与Nastya“直接交换”的人数. 2. 对于排在j前面的i,如果i和i-j之间(包括j)的每个人都能“直接交换”,j才能前进一步. 实现: #include <b ...
- Codeforces Round #546 (Div. 2)-D - Nastya Is Buying Lunch
这道题,神仙贪心题... 题意就是我给出数的顺序,并给出多个交换,每个只能用于相邻交换,问最后一个元素,最多能往前交换多少步. 我们考虑这样一个问题,如果一个这数和a[n]发生交换,那么这个数作为后面 ...
- cf1136D. Nastya Is Buying Lunch(贪心)
题意 题目链接 给出一个排列,以及\(m\)个形如\((x, y)\)的限制,表示若\(x\)在\(y\)之前则可以交换\(x, y\). 问\(n\)位置上的数最多能前进几步 \(n \leqsla ...
- D. Nastya Is Buying Lunch
链接 [https://codeforces.com/contest/1136/problem/D] 题意 有N个人,a[i]表示第i个人的编号,m个二元组. 当前一个在后一个的前面一个位置时二者可以 ...
- Codeforces 1136D - Nastya Is Buying Lunch - [贪心+链表+map]
题目链接:https://codeforces.com/problemset/problem/1136/D 题意: 给出 $1 \sim n$ 的某个排列 $p$,再给出若干 $(x,y)$ 表示当序 ...
- Nastya Is Buying Lunch CodeForces - 1136D (排列)
大意: 给定n排列, m个pair, 每个pair(u,v), 若u,v相邻, 且u在v左侧, 则可以交换u和v, 求a[n]最多向左移动多少 经过观察可以发现, 尽量先用右侧的人与a[n]交换, 这 ...
- Codeforces 1136D Nastya Is Buying Lunch (贪心)
题意: 给一个序列和一组交换序列(a,b),当且仅当a在b的前面(不允许有间隔),这两个数才能交换,问最后一个数最多能移动多少个位置. 分析: 这题是思路是十分的巧妙呀 , 用一个数组num[x] ...
- Codeforces 1136 - A/B/C/D/E - (Done)
链接:https://codeforces.com/contest/1136/ A - Nastya Is Reading a Book - [二分] #include<bits/stdc++. ...
- Codeforces Round #546 (Div. 2) 题解
Codeforces Round #546 (Div. 2) 题目链接:https://codeforces.com/contest/1136 A. Nastya Is Reading a Book ...
随机推荐
- jupyter nootbook本地使用指南
本地文件读入jupyter notebook 在文件夹内,shift+鼠标右键,出现菜单中选择“”在此处打开命令窗口“”,输入jupyter notebook, 可以把本地文件读入jupyter.
- Java裸写爬虫技术,运用多线程技术,高效爬取某个医疗机构网站数据
最近喜欢上了数据的庞大的感觉,就爬取了一下某个医疗机构网站医疗数据,由于数据量庞大,只爬取了江西省的各个市的各个医院的各个科室的各个科室.中各种信息.其中用的持久层技术是hibernate框架,和用到 ...
- 简单迷宫算法(递归与非递归C++实现)
假定迷宫如下:1代表墙,0代表道路,起点在(1,1),终点(11,9)(PS:下标从0开始计算). 现在寻求一条路径能从起点到达终点(非最短). 有两种解法:递归与非递归. 递归算法思路: 要用递归, ...
- gunicorn+anaconda+nginx部署django项目(ubuntu)
首先进入conda 虚拟环境: source activate test 安装gunicorn: pip install gunicorn 运行gunicorn gunicorn -w 2 -b 12 ...
- Luogu P4204 神奇口袋 题解报告
题目传送门 [题目大意] 一个口袋里装了t种颜色的球,第i种颜色的球的数目为a[i],每次随机抽一个小球,然后再放d个这种颜色的小球进口袋. 给出n个要求,第x个抽出的球颜色为y,求满足条件的概率. ...
- 有关mysql索引
1.首先我们需要明确一下什么是索引以及为什么要使用索引: 索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构.在生产环境中,对于数据库我们最常进行的是查询的操作,而当我们的数据非 ...
- 论文学习笔记--无缺陷样本产品表面缺陷检测 A Surface Defect Detection Method Based on Positive Samples
文章下载地址:A Surface Defect Detection Method Based on Positive Samples 第一部分 论文中文翻译 摘要:基于机器视觉的表面缺陷检测和分类可 ...
- docker中的镜像中运行Django项目
首先要在镜像中 安装python3 以及 django2.0.4 目前我用的是这两个版本. 进入镜像 创建项目 进入项目中修改setting文件 将引号和星号添加进括号中 ALLOWED_HOSTS ...
- c++消息队列的实现
#ifndef NET_FRAME_CONCURRENT_QUEUE_H #define NET_FRAME_CONCURRENT_QUEUE_H #include <queue> # ...
- django+vue
django安装配置方式略过 1.安装node.js,官网地址:https://nodejs.org/zh-cn/download/ 2.cd到项目目录下,执行npm install -g vue-c ...