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 ...
随机推荐
- Windchill
判断某查询栏位是否为空 错误:if (projComp != null && !projComp.equals("")) 正确:if((projComp != nu ...
- android activity生命周期的一张经典图片
图片来自http://blog.csdn.net/android_tutor/article/details/5772285 onpause只有弹出的窗体是Activity的时候才会触发,并非是通过焦 ...
- Learning Python 009 dict(字典)和 set
Python dict(字典)和 set dict (字典)是什么东西 dict全称dictionary.为什么这个数据结构取名叫dict.因为dict的实现原理和查字典是一样的.dict使用了键-值 ...
- OpenCV 鼠标手动绘制掩码图像
OpenCV 鼠标手动绘制掩码图像 完整的代码: #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui ...
- JavaScript学习系列8 - JavaScript中的关系运算符
JavaScript中有8个关系运算符,分别是 ===, !===, ==, !=, <, <=, >, >= 1. 恒等运算符 (===) ===也叫做 严格相等运算符,它要 ...
- VS2010和VS2015的Dll项目
最近在使用公司VS2010开发的老的项目时,发现一些问题 公司用VS2010开发了一个项目,生成 GUS_TestIdentity.dll, 放在 C:\Windows\assembly 中 当在另一 ...
- URAL 2018 The Debut Album (DP)
题意:给出n长度的数列,其实1的连续个数不超过a,2的连续个数不超过b. 析:dp[i][j][k] 表示前 i 个数,以 j 结尾,并且连续了k个长度,要用滚动数组,要不然MLE. 代码如下: #p ...
- C# 外观模式
外观模式(Facade Pattern) 介绍 定义: 为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用.使用外观模式时,创建了一个统一的类,用来 ...
- webpack -- 多页面简单小例
有时单页面并不能满足我们的业务需求,就需要去构建多页面应用,以下为简单小例: entry:{ index:'./src/module/index/index.js', student:'./src/m ...
- 【转】processOnServer
源地址:http://blog.csdn.net/dl020840504/article/details/8856853