思路:

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. ansible一键部署LAMP

    一.实现ansible跟节点间无密码访问,不会配置的请看 文章 . 二.创建目录 $ mkdir -p playbooks/{files,templates}   三.创建php测试文件index.p ...

  2. [Uva12260]Free Goodies(dp+贪心)

    解题关键:先对p进行排序,消除p的影响,然后对w进行01背包即可.注意p对w的约束.j<=(cur+1)/2 #include<cstdio> #include<cstring ...

  3. Google浏览器插件

    Scholarscope XPath Helper Web Scraper JSON-handle  User-Agent Swicher  Markdown Here

  4. Linux常用知识点汇总

    常用命令 1.ls 列出目录下的所有文件及文件夹 2.pwd 打印出当前所在目录 3. ./ 执行 .sh 文件命令 4.ip addr 查看ip地址 5.sudo  service network ...

  5. InnoDB信息说明

    InnoDB是MySQL数据库发展至今一款至关重要的数据库存储引擎,其不仅支持事务特性,并且具有丰富的统计信息,便于数据库管理人员了解最近InnoDB存储引擎的运行状态. 早期版本的InnoDB存储引 ...

  6. bootstrap入门案例

    创建文档基本结构, 包括导入CSS,JS bootstrap初学者模板 添加元件 先添加一个导航栏, 直接粘贴即可使用 https://v4.bootcss.com/docs/4.0/componen ...

  7. [Xcode 实际操作]四、常用控件-(5)UILabel文本标签自定义文字样式

    目录:[Swift]Xcode实际操作 本文将演示给标签对象添加描边效果,在项目文件夹上,点击鼠标右键菜单, 选择[Create File]->[Cocoa Touch Class]->[ ...

  8. Go入门基础手记

    1. 配置环境变量(临时) export GOPATH=yourpath 2. 跨平台交叉编译 env GOOS=linux GOARCH=amd64 go build 3. test写法 // 首先 ...

  9. 1、gitlab的理论知识

    2.1 svn与git对比 . svn git 分布式 不是 是 在线阅读 不支持 不仅支持,而且可以在线编辑 存储方式 按文件 按元数据 完整性 一般 优 离线工作 日志都没法看 完全没问题 分支 ...

  10. PAT甲级——1100 Mars Numbers (字符串操作、进制转换)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90678474 1100 Mars Numbers (20 分) ...