思路:

1. 最终答案不超过能与Nastya“直接交换”的人数。

2. 对于排在j前面的i,如果i和i~j之间(包括j)的每个人都能“直接交换”,j才能前进一步。

实现:

 #include <bits/stdc++.h>
using namespace std;
const int N = ;
int a[N], p[N], cnt[N];
vector<int> v[N]; int main()
{
int n, m;
while (cin >> n >> m)
{
for (int i = ; i <= n; i++) v[i].clear();
memset(cnt, , sizeof cnt);
int x, y;
for (int i = ; i <= n; i++) { cin >> a[i]; p[a[i]] = i; }
for (int i = ; i <= m; i++)
{
cin >> x >> y;
v[y].push_back(x);
}
for (int i = n; i >= ; i--)
{
for (int j = ; j < v[a[i]].size(); j++)
{
if (p[v[a[i]][j]] < i) cnt[v[a[i]][j]]++;
}
}
vector<pair<int, int>> t;
for (int i = ; i < v[a[n]].size(); i++)
{
int to = v[a[n]][i];
t.push_back(make_pair(p[to], to));
}
sort(t.begin(), t.end());
int cur = n, ans = ;
for (int i = t.size() - ; i >= ; i--)
{
x = t[i].second;
if (cnt[x] == cur - t[i].first)
{
ans++; cur--;
for (int j = ; j < v[x].size(); j++)
{
int to = v[x][j]; cnt[to]--;
}
}
}
cout << ans << endl;
}
return ;
}

CF1136D Nastya Is Buying Lunch的更多相关文章

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

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

  2. Nastya Is Buying Lunch

    At the big break Nastya came to the school dining room. There are nn pupils in the school, numbered ...

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

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

  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. Flask05 cookie、类视图、方法视图、自己的404页面

    1 什么是cookie 就是网站存放到你浏览器中的一部分固定内容:当你下次访问我这个网站的时候,你会把之前我存放到你浏览器中的数据带回来给我        你要先登录(用户名.密码) ->   ...

  2. 6、R语言绘制带errorbar 的柱状图

    转载:http://www.cnblogs.com/xudongliang/p/7283733.html data <- data.frame(mean = c(10, 15), sd = c( ...

  3. cargo实现自动化部署远程jetty容器(非安全模式)

    cargo实现自动化部署应用至远程jetty容器 (非安全模式) 一.准备: WAR包:Deployer Web application for the Jetty remote containers ...

  4. 网页设计与开发:HTML、CSS、JavaScript实例教程 (郑娅峰) pdf扫描版

    网页设计与开发:HTML.CSS.JavaScript实例教程从实用角度出发,详细讲解了HTML.CSS和JavaScript的基本语法和设计技巧,通过一个实用的班级网站的规划.设计.实现到发布过程, ...

  5. git 仓库拆分方案对比

    此文已由作者张磊授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前言 git 拆分仓库在网上已有的案例上来看,分为 submodule 和 subtree. 还有基于这两个方 ...

  6. Linux中网卡相关命令以及SSH连接远程主机

    ifconfig 命令 查看与配置网络状态命令 关闭与启动网卡 ifdown 网卡设备名 禁用该网卡设备 ifup 网卡设备名 启用该网卡设备 查询网络状态 netstat 选项 -t 列出TCP协议 ...

  7. GoWeb开发_Iris框架讲解(三):路由功能处理方式

    Context概念 Context是iris框架中的一个路由上下文对象,在iris框架中的源码路径定义为:{$goPath}\github.com\kataras\iris\context\conte ...

  8. [Cqoi2014]危桥 (两遍网络流)

    题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { , ...

  9. Eclipse中新建Maven Web项目报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    在maven web项目中的index.jsp中的错误信息如下: The superclass "javax.servlet.http.HttpServlet" was not f ...

  10. Observer模式(观察者设计模式)

    Observer 设计模式? 在Observer模式中,当观察对象的状态发生变化时,会通知给观察者.Observer模式适用于根据对象状态进行相应处理的场景. Observer 并非主动观察,而是被动 ...