Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D
贪心 + 思维
题意
你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则,假如u在v相邻前面,那么u和v可以交换位置,问你是队列最后一个人的时候你最前可以换到前面哪里
题解
- 因为相邻才能换,所以最后一个换到前面一定是一步一步向前走,所以不存在还要向后走的情况
- 设最后一个为u,假设前面有一个能和u换位置的集合,那么需要将这些点尽量往后移动去接u
- 假设前面有一个不能和u换位置的集合S,那么u和S的顺序永远不会换过来
- 从后向前遍历,对于每个点假设后面紧接着S+u,假如这个点能和S+u换位置,那么u可以向前移动一格,ans++
- 假如不行,则将这个加入S
#include<bits/stdc++.h>
using namespace std;
int n,m,a[300005],u,v,i,ok,ans;
vector<int>A;set<pair<int,int> >vi;
int main(){
cin>>n>>m;
for(i=1;i<=n;i++)cin>>a[i];
for(i=0;i<m;i++){
cin>>u>>v;
vi.insert(make_pair(u,v));
}
A.push_back(a[n]);
for(i=n-1;i>=1;i--){
ok=1;
for(auto x:A){
if(!vi.count(make_pair(a[i],x))){ok=0;break;}
}
if(ok)ans++;
else A.push_back(a[i]);
}
cout<<ans;
}
Codeforces Round #546 (Div. 2) D 贪心 + 思维的更多相关文章
- Codeforces Round #546 (Div. 2) 题解
Codeforces Round #546 (Div. 2) 题目链接:https://codeforces.com/contest/1136 A. Nastya Is Reading a Book ...
- Codeforces Round #546 (Div. 2)D(贪心,思维,SET,VECTOR,模拟)
#include<bits/stdc++.h>using namespace std;int a[300007],b[500007],c[500007];set<int>st[ ...
- Codeforces Round #547 (Div. 3) F 贪心 + 离散化
https://codeforces.com/contest/1141/problem/F2 题意 一个大小为n的数组a[],问最多有多少个不相交的区间和相等 题解 离散化用值来做,贪心选择较前的区间 ...
- Codeforces Round #595 (Div. 3)D1D2 贪心 STL
一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...
- Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索
https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...
- Codeforces Round #303 (Div. 2) D 贪心
D. Queue time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】
A. Saitama Destroys Hotel time limit per test:1 second memory limit per test:256 megabytes input:sta ...
- Codeforces Round #546 (Div. 2)-D - Nastya Is Buying Lunch
这道题,神仙贪心题... 题意就是我给出数的顺序,并给出多个交换,每个只能用于相邻交换,问最后一个元素,最多能往前交换多少步. 我们考虑这样一个问题,如果一个这数和a[n]发生交换,那么这个数作为后面 ...
- Codeforces Round #545 (Div. 2) D 贪心 + kmp
https://codeforces.com/contest/1138/problem/D 题意 两个01串s和t,s中字符能相互交换,问最多能得到多少个(可交叉)的t 题解 即将s中的01塞进t中, ...
随机推荐
- swift - 正则表达式
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...
- how2j网站前端项目——天猫前端(第一次)学习笔记2
今天早上开始首页内容.首页除了公共页面,还有许多自己的内容:导航和轮播.分类菜单.推荐产品展示,最后还有js的互动. 一.导航和轮播的学习 在自己做图片的轮播时,还是没有一次成功.存在了好几处问题: ...
- jq里面关于disable的用法
//两种方法设置disabled属性$('#areaSelect').attr("disabled",true);$('#areaSelect').attr("disab ...
- YII2 在使用控制器调试微信接口时报错 Unable to verify your data submission
出现这个原因,是因为提交数据时,被YII2的CSRF验证给拦截了. 只需要在当前控制器设置一个属性,关闭该验证就可以了. public $enableCsrfValidation = false;
- linq join用法
单条件: var query = from person in people join pet in pets on person equals pet.Owner select new { Owne ...
- mysql --secure-file-priv is set to NULL.Operations related to importing and exporting data are disabled
--secure-file-priv is set to NULL. Operations related to importing and exporting data are disabledmy ...
- 3A - Holding Bin-Laden Captive!
We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But rec ...
- js 闭包 弊端
闭包有许多有趣的用途,Javascript的两个特征使它这么有趣:1. function是一个对象,它跟数组,Object一样,地位平等.2. Javascript变量作用域范围.<Javasc ...
- mysql if--then--else --endif 问题
if 0 =resultValue then set @m = 2; else set @m =1; end if if 0 =resultValue then set @m = 2; else se ...
- Codeforces 749C. Voting 模拟题
C. Voting time limit per test: 1 second memory limit per test: 256 megabytes input: standard input o ...