BZOJ2535: [Noi2010]Plane 航空管制2(拓扑排序 贪心)
题意
Sol
非常妙的一道题。
首先不难想到拓扑排序,但是直接对原图按\(k\)从小到大拓扑排序是错的。因为当前的\(k\)大并不意味着后面的点\(k\)也大
但是在反图上按\(k\)从大到小拓扑排序就是对的。为什么呢?因为题目中给出的条件是下限, 而在反图上拓扑排序就相当于卡着下限做,因此一定是最优的
对于第二问,同样在反图上搞。对每个点分开做,贪心的策略是:如果有其他的飞机可以起飞则让他们起飞,直到没有飞机可以起飞,这时的时间就是答案
// luogu-judger-enable-o2
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
using namespace std;
const int MAXN = 2e5 + 10;
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, a[MAXN], inder[MAXN], tmp[MAXN], ans[MAXN];
vector<int> v[MAXN];
void Topsort() {
priority_queue<Pair> q;
for(int i = 1; i <= N; i++)
if(!inder[i]) q.push(MP(a[i], i));
int tot = 0;
while(!q.empty()) {
int p = q.top().se; q.pop(); ans[++tot] = p;
for(int i = 0, to; i < v[p].size(); i++) {
to = v[p][i];
inder[to]--;
if(!inder[to]) q.push(MP(a[to], to));
}
}
for(int i = tot; i >= 1; i--) printf("%d ", ans[i]); puts("");
}
int solve(int x) {
memcpy(inder, tmp, sizeof(tmp));
priority_queue<Pair> q;
inder[x] = N;
for(int i = 1; i <= N; i++) if(!inder[i]) q.push(MP(a[i], i));
int tim = N;
for(int i = N; i; i--) {
if(q.empty() || (q.top().fi < i)) return i;
int p = q.top().se; q.pop();
for(int i = 0, to; i < v[p].size(); i++) {
to = v[p][i];
inder[to]--;
if(!inder[to]) q.push(MP(a[to], to));
}
}
return tim;
}
int main() {
N = read(); M = read();
for(int i = 1; i <= N; i++) a[i] = read();
for(int i = 1; i <= M; i++) {
int x = read(), y = read();
v[y].push_back(x); inder[x]++; tmp[x]++;
}
Topsort();
for(int i = 1; i <= N; i++) printf("%d ", solve(i));
return 0;
}
/*
10 10
4 4 3 6 9 9 10 7 10 7
2 9
3 5
6 7
1 5
7 9
10 2
3 8
8 6
3 10
8 5
*/
BZOJ2535: [Noi2010]Plane 航空管制2(拓扑排序 贪心)的更多相关文章
- BZOJ2535 [Noi2010]Plane 航空管制2
Description 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上 ...
- BZOJ2535 [Noi2010]Plane 航空管制 【贪心 + 堆】
题目链接 BZOJ2535 题解 航班之间的关系形成了一个拓扑图 而且航班若要合法,应尽量早出发 所以我们逆拓扑序选点,能在后面出发的尽量后面出发,不会使其它点变得更劣,容易知是正确的 第二问只需枚举 ...
- BZOJ2109: [Noi2010]Plane 航空管制
Description 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频 发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此, 小X表示很不满意. 在这次来烟台的 ...
- 2109&2535: [Noi2010]Plane 航空管制 - BZOJ
Description世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上, ...
- BZOJ 2535: [Noi2010]Plane 航空管制2
Description 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上 ...
- bzoj 2109: [Noi2010]Plane 航空管制
Description 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频 发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此, 小X表示很不满意. 在这次来烟台的 ...
- bzoj 2535: [Noi2010]Plane 航空管制2【拓扑排序+堆】
有个容易混的概念就是第一问的答案不是k[i]字典序最小即可,是要求k[i]大的尽量靠后,因为这里前面选的时候是对后面有影响的(比如两条链a->b c->d,ka=4,kb=2,kc=3,k ...
- bzoj 2535 && bzoj 2109 [Noi2010]Plane 航空管制——贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2535 https://www.lydsy.com/JudgeOnline/problem.p ...
- 【BZOJ2109/2535】【NOI2010】航空管制(贪心)
[BZOJ2109/2535][NOI2010]航空管制(贪心) 题面 BZOJ2109 BZOJ2535 题解 很好玩的一道题目 先看第一问,显然是要找一个合法的拓扑排序的序列. 直接拓扑排序,把队 ...
随机推荐
- 利用PHP 简单实现加减法验证码
<?php header('Content-Type: image/png'); $im = imagecreatetruecolor( 200 , 50 );//生成图片长宽 // Creat ...
- 重写成员“MySql.Data.Entity.MySqlConnectionFactory.CreateConnection(System.String)”时违反了继承安全性规则。重写方法的安全可访问性必须与所重写方法的安全可访问性匹配。
1,程序中使用加载反射出现下面的问题: 无法加载一个或多个请求的类型.有关更多信息,请检索 LoaderExceptions 属性. 然后把代码改了一下, try { types.AddRange ...
- 剩下的树 THU 机试
链接:https://www.nowcoder.com/questionTerminal/f5787c69f5cf41499ba4706bc93700a2来源:牛客网 有一个长度为整数L(1<= ...
- Android Activity实例应用(选择QQ头像)
1.效果图 点击button,跳转到页面2 选择需要的头像,自动返回 3.XML文件布局 页面1 <?xml version="1.0" encoding="utf ...
- Flask之flask-migrate 数据库迁移
简介 flask-migrate是flask的一个扩展模块,主要是扩展数据库表结构的. 官方文档:http://flask-migrate.readthedocs.io/en/latest/ 使用fl ...
- while循环、运算符和格式化输出以及编码
一.while循环 1.while就是当的意思,while指当其后面的条件成立,就执行while下面的代码 写一段代码让程序从0打印到100的程序,每次循环+1. count = 0 while co ...
- JS window,onload 与 $().read()
JS:window.onload的使用介绍 .在body标签里面 .在JS语句调用 .同时调用多个函数 .JS调用多个函数 .自定义的函数多次调用 jquery $(document).ready() ...
- drf之视图案例
views.py from django.shortcuts import render # Create your views here. from rest_framework.generics ...
- python re模块 collections模块
根据手机号码一共11位并且是只以13.14.15.18开头的数字这些特点,我们用python写了如下代码: while True: phone_number = input('please input ...
- date +%F时间日期
date +%Y -%m-%d 年月日 date +%T 显示时间 HMS几点几分几秒 -%H 为小时 %w 周几 date -d “-1day” 一天之前 date ...