题意

题目链接

给出一个排列,以及\(m\)个形如\((x, y)\)的限制,表示若\(x\)在\(y\)之前则可以交换\(x, y\)。

问\(n\)位置上的数最多能前进几步

\(n \leqslant 3* 10^5, m \leqslant 5 * 10^5\)

Sol

每次遇到这种动来动去的题基本都做不出来qwq

我最开始想到的是图论模型,然后发现不管怎么建都有反例。结果标算是个神仙贪心?。。

考虑这样一种贪心:从前往后处理每一个数,记一个\(num\)数组表示该位置的数最多能往后移动几次,每次把当前数的限制条件加到\(num\)里。如果当前的\(num\)大于等于和开始时最后一个数的距离,那么\(ans++\)。(好像看代码会更明白一些)

#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long
#define LL long long
#define ull unsigned long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
template <typename A, typename B> inline LL fp(A a, B p, int md = mod) {int b = 1;while(p) {if(p & 1) b = mul(b, a);a = mul(a, a); p >>= 1;}return b;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, p[MAXN], num[MAXN];
vector<int> v[MAXN];
signed main() {
N = read(); M = read();
for(int i = 1; i <= N; i++) p[i] = read();
for(int i = 1; i <= M; i++) {
int x = read(), y = read();
v[y].push_back(x);
}
for(auto &x : v[p[N]]) num[x]++;
int ans = 0;
for(int i = N - 1; i >= 1; i--) {
if(num[p[i]] >= N - ans - i) ans++;
else for(auto &x : v[p[i]]) num[x]++;
}
cout << ans;
return 0;
}

cf1136D. Nastya Is Buying Lunch(贪心)的更多相关文章

  1. CF1136D Nastya Is Buying Lunch

    思路: 1. 最终答案不超过能与Nastya“直接交换”的人数. 2. 对于排在j前面的i,如果i和i-j之间(包括j)的每个人都能“直接交换”,j才能前进一步. 实现: #include <b ...

  2. Codeforces 1136D - Nastya Is Buying Lunch - [贪心+链表+map]

    题目链接:https://codeforces.com/problemset/problem/1136/D 题意: 给出 $1 \sim n$ 的某个排列 $p$,再给出若干 $(x,y)$ 表示当序 ...

  3. Codeforces 1136D Nastya Is Buying Lunch (贪心)

    题意: 给一个序列和一组交换序列(a,b),当且仅当a在b的前面(不允许有间隔),这两个数才能交换,问最后一个数最多能移动多少个位置. 分析: 这题是思路是十分的巧妙呀 , 用一个数组num[x]  ...

  4. Nastya Is Buying Lunch

    At the big break Nastya came to the school dining room. There are nn pupils in the school, numbered ...

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

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

  6. D. Nastya Is Buying Lunch

    链接 [https://codeforces.com/contest/1136/problem/D] 题意 有N个人,a[i]表示第i个人的编号,m个二元组. 当前一个在后一个的前面一个位置时二者可以 ...

  7. Nastya Is Buying Lunch CodeForces - 1136D (排列)

    大意: 给定n排列, m个pair, 每个pair(u,v), 若u,v相邻, 且u在v左侧, 则可以交换u和v, 求a[n]最多向左移动多少 经过观察可以发现, 尽量先用右侧的人与a[n]交换, 这 ...

  8. 【洛谷】P2983 [USACO10FEB]购买巧克力Chocolate Buying(贪心)

    题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...

  9. 【BZOJ】2014: [Usaco2010 Feb]Chocolate Buying(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2014 这应该是显然的贪心吧,先排序,然后按花费取 #include <cstdio> # ...

随机推荐

  1. springboot下载文件

    例子 /** * 下载假期模板 */ @ResponseBody @RequestMapping(value = "/downloadDolidayTemplate.do") pu ...

  2. Spring详解(六)------AspectJ 实现AOP

    上一篇博客我们引出了 AOP 的概念,以及 AOP 的具体实现方式.但是为什么要这样实现?以及提出的切入点表达式到底该怎么理解? 这篇博客我们通过对 AspectJ 框架的介绍来详细了解. 1.什么是 ...

  3. Ubuntu释放磁盘空间的几种常用方法

    一 安装stacer,使用它来清空系统内存 其实 Stacer 的安装步非常简单,只需到 Github 的发布页面下载到 .deb 包,再用 GDebi 或如下命令安装即可: wget https:/ ...

  4. JWT(Json web token)简介

    Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点的单点登录(S ...

  5. web容器 web服务器 servlet/jsp容器 之间的区别和关系

    今天学习tomcat时发现一篇写的比较好的文章,故分享给大家 Web服务器(软件): Apache http server, 这个它的网址,http://httpd.apache.org/downlo ...

  6. 利用 ELK 搭建 Docker 容器化应用日志中心

    利用 ELK 搭建 Docker 容器化应用日志中心 概述 应用一旦容器化以后,需要考虑的就是如何采集位于 Docker 容器中的应用程序的打印日志供运维分析.典型的比如SpringBoot应用的日志 ...

  7. 通过Calendar简单解析Date日期,获取年、月、日、星期的数值

    有时候项目中需要用到Date的年.月.日.星期的数值.那么解析方法如下: /**解析日期,获取年月日星期*/ private void parseDateToYearMonthDayWeek(Date ...

  8. 《JavaScript总结》js模块化

    模块化开发,可以让代码易于扩展.便于日后维护. ES6中的模块化 我们先了解一下 export(导出) 和 import(导入) 这两个关键字. 新建一个文件a.js 并且导出变量test expor ...

  9. vue-14-less 语法的使用

    vue-15-rem-less 在计算手机端页面的时候, 使用rem和less的方式, 可以方便的设置需要的大小等 1, 在index.html中添加rem的script 代码 在head中添加 &l ...

  10. kibana从入门到精通-Kibana安装

    作者其他ELK快速入门系列文章 Elasticsearch从入门到精通 logstash快速入门实战指南 简介 Kibana 是一款开源的数据分析和可视化平台,它是 Elastic Stack 成员之 ...