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 ...
随机推荐
- Spring开发包介绍
-----------------siwuxie095 核心开发包 建立 Spring 工程时,需要引入 Spring 的开发包,否则无 ...
- web 系统原理之路由器和交换机的区别
工作层次不同: 交换机主要工作在数据链路层(第二层) 路由器工作在网络层(第三层). 转发依据不同: 交换机转发所依据的对象时:MAC地址.(物理地址) 路由转发所依据的对象是:IP地址.(网络地址) ...
- 修改TABLE中的Column的属性
删除主键名 这个主键名并不是列名,而是主键约束的名字,如果当时设置主键约束时没有制定约束的名字 设置主键的语句:ALTER TABLE P add constraint pk PRIMARY KEY ...
- SQLServer数据库权限设置--保障数据库安全
一.登陆界面引入 下图为SQL Server的登陆界面. 1)服务器名称:“.”代表本地计算机,选择下拉框,可以看见还有一个与本机机名相同的内容,也代表于本地服务器连接:要连接远程服务器的话,在此处填 ...
- 第一个Python工程
创建你的第一个Python程序 如果你曾经很熟悉visual studio的工作方式.可能对python不习惯. 工程通常只与你使用的IDLE有关系.这些工具习惯将文档,编译,测试集成一体.所以就存在 ...
- Git fatal:$'GIT_DIR' too big
这句表示的是创建的文件的名称太长了,将你需要放置的代码的文件名字改短一些即可!
- [CentOS7] minimal安装后 出现 没有ifconfig 无法ping 无法yum could not retrieve mirrorlist http://mirrorlist.centos.org/
刚以minimal方式安装完CentOS,打算看下ip,结果ifconfig没找到(后来得知可以用ip addr查看本机ip) 于是yum grouplist, 结果出现could not retri ...
- Extensions for Vue
Extensions for Vue Original post url:https://www.cnblogs.com/markjiang7m2/p/10833790.html If you are ...
- 在mac上使用sublime text3搭建opencv3开发环境
安装sublime text3 打开mac终端,安装brew 安装opencv3,终端输入下面的coomand: brew install opencv@3 注意:@3表示安装的版本,如果不加@3,那 ...
- Pod中spec的字段常用字段及含义
一.Pod中spec的字段常用字段及含义 1.pod.spec.containers ² spec.containers.name <string> #pod的名称,必须字段,名称唯一 ...