大意: 给定n排列, m个pair, 每个pair(u,v), 若u,v相邻, 且u在v左侧, 则可以交换u和v, 求a[n]最多向左移动多少

经过观察可以发现, 尽量先用右侧的人与a[n]交换, 这样一定最优, 然后从右往左遍历, 假设a[n]当前在$pos$, 再观察可以发现, 若$a[x]$产生贡献, 当且仅当$[x+1,pos]$都与$x$连边, 然后统计一下边数就可以$O(n+m)$了

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e6+10;
int n, m;
int a[N], cnt[N];
vector<int> g[N]; int main() {
scanf("%d%d", &n, &m);
REP(i,1,n) scanf("%d", a+i);
REP(i,1,m) {
int x, y;
scanf("%d%d", &x, &y);
g[y].pb(x);
}
for (auto &t:g[a[n]]) ++cnt[t];
int ans = 0;
PER(i,1,n-1) {
if (n-i-ans==cnt[a[i]]) ++ans;
else {
for (auto &t:g[a[i]]) ++cnt[t];
}
}
printf("%d\n", ans);
}

Nastya Is Buying Lunch CodeForces - 1136D (排列)的更多相关文章

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

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

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

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

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

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

  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. cf1136D. Nastya Is Buying Lunch(贪心)

    题意 题目链接 给出一个排列,以及\(m\)个形如\((x, y)\)的限制,表示若\(x\)在\(y\)之前则可以交换\(x, y\). 问\(n\)位置上的数最多能前进几步 \(n \leqsla ...

  6. D. Nastya Is Buying Lunch

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

  7. CF1136D Nastya Is Buying Lunch

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

  8. Pavel and barbecue CodeForces - 756A (排列,水题)

    大意: 给定排列p, 0/1序列b, 有n个烤串, 每秒钟第i串会移动到$p_i$, 若$p_i$为1则翻面, 可以修改b和p, 求最少修改次数使得每串在每个位置正反都被烤过. 显然只需要将置换群合并 ...

  9. Nastya and a Wardrobe CodeForces - 992C(规律)

    写一下二叉树  推一下公式就出来了, 注意取模时的输出形式 #include <bits/stdc++.h> #define mem(a, b) memset(a, b, sizeof(a ...

随机推荐

  1. Linux基础命令---fsck

    fsck 检查或者修复指定的文件系统,可以是设备名.挂载点,还可以是一个ext2的label,或者是一个UUID.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.ope ...

  2. 深入hibernate的三种状态(转)

    hibernate的三种状态: 瞬时对象,持久化对象,托管对象. hibernate的两级缓存:1>一级缓存:session    2>二级缓存:sessionfactory. 瞬时对象: ...

  3. (mac)阿里云ECS服务器配置过程

    -----首先本人是半只脚入门的iOS开发者,弄这个只是单纯想多学点东西. -----阿里云服务器的配置选择:既然是学习用的,最最基础的配置就行了.1M带宽,1核1G...这就不详述了.没啥可选的,( ...

  4. 手撕vue-cli配置——utils.js篇

    utils.js文件主要是用来处理各种css loader的,比如css-loader,less-loader等. //引入path模块 const path = require('path') // ...

  5. ELK学习笔记之ELK分析nginx日志

    0x00 配置FIlebeat搜集syslog #安装 rpm -ivh filebeat-6.2.3-x86_64.rpm mv /etc/filebeat/filebeat.yml /etc/fi ...

  6. Python入门之Python引用模块和查找模块路径

    #这篇文章主要介绍了Python引用模块和Python查找模块路径的相关资料,需要的朋友可以参考下 模块间相互独立相互引用是任何一种编程语言的基础能力.对于“模块”这个词在各种编程语言中或许是不同的, ...

  7. jQuery API的特点

    jQuery API 的特点 版权声明:未经博主授权,严禁转载分享 jQuery API 的三大特点 1. jQuery 对象是一个类数组对象,API自带遍历效果 - 对 jQuery 对象调用一次A ...

  8. win7下配置IIS

    Internet Information Services(IIS,互联网信息服务),是由微软公司提供的基于运行Microsoft Windows的互联网基本服务.最初是Windows NT版本的可选 ...

  9. 论文笔记——Channel Pruning for Accelerating Very Deep Neural Networks

    论文地址:https://arxiv.org/abs/1707.06168 代码地址:https://github.com/yihui-he/channel-pruning 采用方法 这篇文章主要讲诉 ...

  10. Win7上安装WMware虚拟机和Ubuntu操作系统

    效果图: 问题拾遗: 一.如何划分一个新硬盘空间? 参考链接:如何新建磁盘空间 效果图: 我划分了20G的内存空间给Ubuntu的硬盘空间.一般来说15G就够用了. 二.VMware上拷贝Ubuntu ...