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 贪心 + 思维的更多相关文章

  1. Codeforces Round #546 (Div. 2) 题解

    Codeforces Round #546 (Div. 2) 题目链接:https://codeforces.com/contest/1136 A. Nastya Is Reading a Book ...

  2. 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[ ...

  3. Codeforces Round #547 (Div. 3) F 贪心 + 离散化

    https://codeforces.com/contest/1141/problem/F2 题意 一个大小为n的数组a[],问最多有多少个不相交的区间和相等 题解 离散化用值来做,贪心选择较前的区间 ...

  4. Codeforces Round #595 (Div. 3)D1D2 贪心 STL

    一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...

  5. Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索

    https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...

  6. Codeforces Round #303 (Div. 2) D 贪心

    D. Queue time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  7. 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 ...

  8. Codeforces Round #546 (Div. 2)-D - Nastya Is Buying Lunch

    这道题,神仙贪心题... 题意就是我给出数的顺序,并给出多个交换,每个只能用于相邻交换,问最后一个元素,最多能往前交换多少步. 我们考虑这样一个问题,如果一个这数和a[n]发生交换,那么这个数作为后面 ...

  9. Codeforces Round #545 (Div. 2) D 贪心 + kmp

    https://codeforces.com/contest/1138/problem/D 题意 两个01串s和t,s中字符能相互交换,问最多能得到多少个(可交叉)的t 题解 即将s中的01塞进t中, ...

随机推荐

  1. @RequestParam使用须知

    --------------------------siwuxie095                             @RequestParam 使用须知         使用 @Requ ...

  2. HBase数据库集群配置【转】

    https://www.cnblogs.com/ejiyuan/p/5591613.html HBase简介 HBase是Apache Hadoop中的一个子项目,是一个HBase是一个开源的.分布式 ...

  3. 数据库存储 datetime,时差问题

    var offset = moment().utcOffset(); var localText = moment.utc(datetime from database).utcOffset(offs ...

  4. 建立SSH的信任关系

    1.在Client上root用户执行ssh-keygen命令,生成建立安全信任关系的证书.  Client端 # ssh-keygen -t rsa Generating public/private ...

  5. config/index.js

    // see http://vuejs-templates.github.io/webpack for documentation.var path = require('path') module. ...

  6. 关于变量参数的传递,python让人蛋痛的地方

    def find_file(file_table): with open(file_table, 'r', encoding='utf-8') as read_f: line_dict = {} fo ...

  7. 比特币学习-Transaction的locktime属性

    Locktime, also known as nLockTime from the variable name used in the reference client, defines the e ...

  8. 4A - 排序

    输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若干个‘0’组成的,这时这个整数 ...

  9. slam14讲证明构成李代数

  10. Linux CentOS 7 & JDK 1.7 安装与配置

    前言 简单记录一下在CentOS 7中安装配置JDK 1.7的全过程~ 下载 首先是jdk 1.7 64bit & 32bit的下载地址: jdk-7u79-linux-x64.tar.gz ...