CF1136D Nastya Is Buying Lunch
思路:
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的更多相关文章
- cf1136D. Nastya Is Buying Lunch(贪心)
题意 题目链接 给出一个排列,以及\(m\)个形如\((x, y)\)的限制,表示若\(x\)在\(y\)之前则可以交换\(x, y\). 问\(n\)位置上的数最多能前进几步 \(n \leqsla ...
- Nastya Is Buying Lunch
At the big break Nastya came to the school dining room. There are nn pupils in the school, numbered ...
- Codeforces Round #546 (Div. 2)-D - Nastya Is Buying Lunch
这道题,神仙贪心题... 题意就是我给出数的顺序,并给出多个交换,每个只能用于相邻交换,问最后一个元素,最多能往前交换多少步. 我们考虑这样一个问题,如果一个这数和a[n]发生交换,那么这个数作为后面 ...
- 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 ...
随机推荐
- 项目积累demo-01
1 搭建Spring-Boot项目 在这里我使用intellij新建spring boot工程: 点击next; 输入Group以及artifact之后.点击next.之后点击web.接着finish ...
- 第二篇elasticsearch配置
1.去github搜索 elashsearch——head,以mobz开头的2.在根目录下安装npm install 3.修改elashsearch下的config文件下的elashsearch.yu ...
- 使用MySQL客户端登录Ensemble数据库查询相关信息
Ensemble公共MySQL数据库 对于大量数据和更详细的分析,Ensemble的MySQL服务器ensembldb.ensembl.org,useastdb.ensembl.org或asiadb. ...
- Win7常用但是被忽略的快捷键
General keyboard shortcuts 1.Ctrl + Right Arrow Move the cursor to the beginning of the next word 向 ...
- OC官方文档翻译-Working-with-Protocols-协议的使用
查看全部文档翻译,请浏览https://github.com/L1l1thLY/Programming-with-Objective-C-in-Chinese,blog仅收录本人翻译的两章. 简述 在 ...
- 令牌Token和会话Session原理与攻略
本篇文章将从无到完整的登录框架或API详细讲述登录令牌原理.攻略等安全点. 有些协议或框架也喜欢把令牌叫票据(Ticket),不论是APP还是Web浏览器,很多框架或协议都用到了本文所说的这套类似的认 ...
- OpenStack基础知识-virtualenv工具详解
1.virtualenv介绍 virtualenv通过创建一个单独的虚拟化python运行环境,将我们所需的依赖安装进去,不同项目之间相互不干扰,从而解决不同的项目之间依赖不同,造成的冲突问题 2.安 ...
- C#正则表达式快速入门
作者将自己在学习正则表达式中的心得和笔记作了个总结性文章,希望对初学C#正则表达式的读者有帮助. [内容] 什么是正则表达式 涉及的基本的类 正则表达式基础知识 构建表达式基本方法 编写一个检验程序 ...
- SSM+Druid的搭建
SSM+druid开发配置 工程目录 1.先从pom文件开始吧 <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...
- linux文件查找find
一.locate locate基于数据库索引来查找文件,数据库在开机时一段时间对更新,不会实时更新,数据库存放在(/var/lib/mlocate/mlocate.db),可以用updatedb来手动 ...