NOI2010 航空管制
http://www.lydsy.com/JudgeOnline/problem.php?id=2535
贪心。
对于第1个问,我们先建立拓扑图,对于如果a必须在b前起飞,那么连有向边b->a,并求出点的入度。
将所有入度为0的点放在一个优先队列里,按最大起飞编号从大到小排序。
我们从后往前考虑起飞的航班。
取出优先队列中最大起飞编号最大的点,作为最后一个航班,并删去拓扑图中与他相连的边,如果有新的点的入度变成0,继续加入优先队列里。
重复操作次即可。
如果问第i个航班最早可以什么时候起飞,我们可以在优先队列中,如果发现最大起飞编号最大的点是第i号航班,我们看看最大起飞编号第二大的点能不能先放,如果能就放最大起飞编号第二大的点;否则这个位置就是第i个航班最早可以起飞的位置了。
时间复杂度O(NM+N^2logN)。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj using namespace std; typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP; #define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b) for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define SF scanf
#define PF printf
#define two(k) (1<<(k)) template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} const DB EPS=1e-;
inline int sgn(DB x){if(abs(x)<EPS)return ;return(x>)?:-;}
const DB Pi=acos(-1.0); inline int gint()
{
int res=;bool neg=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){neg=;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return (neg)?-res:res;
}
inline LL gll()
{
LL res=;bool neg=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){neg=;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return (neg)?-res:res;
} const int maxn=;
const int maxm=; int n,m;
int last[maxn+]; int du[maxn+];
int now,first[maxn+];
struct Tedge{int v,next;}edge[maxm+];
int used[maxn+];
int out[maxn+]; inline void addedge(int u,int v){now++;edge[now].v=v;edge[now].next=first[u];first[u]=now;} struct cmp{inline bool operator ()(PII a,PII b){return a.se<b.se;}};
priority_queue<PII,vector<PII>,cmp> Q; int main()
{
freopen("plane.in","r",stdin);
freopen("plane.out","w",stdout);
int i,j;
n=gint();m=gint();
re(i,,n)last[i]=gint();
mmst(first,-);now=-;
re(i,,m){int a=gint(),b=gint();addedge(b,a);} re(i,,n)du[i]=used[i]=;
re(i,,now)du[edge[i].v]++;
re(i,,n)if(du[i]==)Q.push(PII(i,last[i]));
red(j,n,)
{
int id=Q.top().fi,v;Q.pop();
used[j]=id;
for(i=first[id],v=edge[i].v;i!=-;i=edge[i].next,v=edge[i].v){du[v]--;if(du[v]==)Q.push(PII(v,last[v]));}
}
re(i,,n)PF("%d ",used[i]);PF("\n"); int t;
re(t,,n)
{
re(i,,n)du[i]=used[i]=;
while(!Q.empty())Q.pop();
re(i,,now)du[edge[i].v]++;
re(i,,n)if(du[i]==)Q.push(PII(i,last[i]));
red(j,n,)
{
int id=Q.top().fi,v;Q.pop();
if(id==t)
{
if(!Q.empty() && j<=Q.top().se)
{
int newid=Q.top().fi;Q.pop();
used[j]=newid;
Q.push(PII(id,last[id]));
}
else
{
out[t]=j;
break;
}
}
else
used[j]=id;
for(i=first[used[j]],v=edge[i].v;i!=-;i=edge[i].next,v=edge[i].v){du[v]--;if(du[v]==)Q.push(PII(v,last[v]));}
}
}
re(i,,n)PF("%d ",out[i]);PF("\n");
return ;
}
NOI2010 航空管制的更多相关文章
- NOI2010航空管制
2008: [Noi2010]航空管制 Time Limit: 10 Sec Memory Limit: 552 MBSubmit: 31 Solved: 0[Submit][Status] De ...
- [BZOJ2109][NOI2010]航空管制(贪心+拓扑)
2109: [Noi2010]Plane 航空管制 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1227 Solved: 510[Submit][ ...
- bzoj2535 [Noi2010]航空管制
Description 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上 ...
- [NOI2010]航空管制(拓扑排序+贪心)
题目描述 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上,小X不幸又一 ...
- BZOJ.2109.[NOI2010]航空管制(拓扑 贪心)
题目链接 双倍经验(没有第一问) \(Description\) \(Solution\) 第一问拓扑排序即可. 第二问,即让一个元素在拓扑序中尽量靠前,好像不好做. 但是可以让一个元素出现尽量靠后. ...
- 【[NOI2010]航空管制】
关于拓扑排序的反建图还是一个非常套路的东西 比如说[HNOI2015]菜肴制作 我们希望使得某一个东西在拓扑序中出现的尽可能早,这个时候就可以建出一张反图来,使得这个东西在反图中的拓扑序尽量靠后,从而 ...
- 洛谷 P1954 [NOI2010]航空管制
https://www.luogu.org/problemnew/show/P1954 拓扑排序, 注意到如果正着建图("a出现早于b"=>"a向b连边" ...
- BZOJ 2535:NOI 2010 航空管制
[NOI2010]航空管制 题面请点上面. 首先第一问,我第一想法是把它放到一个小根堆中,然而这是不行的. 正确的思路是,把图反过来建,然后放到一个大根堆里去. 至于原因,感性理解一下,正着贪是有后效 ...
- 【BZOJ2109/2535】【NOI2010】航空管制(贪心)
[BZOJ2109/2535][NOI2010]航空管制(贪心) 题面 BZOJ2109 BZOJ2535 题解 很好玩的一道题目 先看第一问,显然是要找一个合法的拓扑排序的序列. 直接拓扑排序,把队 ...
随机推荐
- 如何实现数字lcd显示效果(原创)
如题,我最先想到的是找一种字体,然后来显示lcd的效果,但是字体又无法满足有空位的时候那个暗灰色的文字的效果,如下所示 就是前三位那些灰色的888,因为你设置数值的时候只能是从0-9的数字,而这灰色的 ...
- 配置PPTP服务器
1.验证内核是否加载了MPPE模块: modprobe ppp-compress-18 && echo MPPE is ok 2.安装所需的软件包: yum -y install pp ...
- IPython notebook 使用介绍
参考资料: http://mindonmind.github.io/2013/02/08/ipython-notebook-interactive-computing-new-era/ http:// ...
- Mosquitto安装_Ubuntu/Debian上安装消息队列Mosquitto
Mosquitto安装_Ubuntu/Debian上安装消息队列Mosquitto MQTT是IBM开发的一个即时通讯协议.MQTT是面向M2M和物联网的连接协议,采用轻量级发布和订阅消息传输机制.M ...
- nyoj 756 重建二叉树
重建二叉树主要是给你一颗二叉树的前序遍历的结果和中序遍历的结果或者后序遍历的结果或者中序遍历的结果,让你求出其中的后序遍历的结果或者前序遍历的结果,这里知道其中的两个就能求出第三个,但是知道的两个必须 ...
- U1总结
import java.io.Writer; import java.util.Iterator; import javax.xml.transform.TransformerFactory; imp ...
- ado.net(1)
connection对象处于最顶层,是所有数据请求的关口 数据库连接过程 SqlConnection theConnection = new SqlConnection(); //创建一个connec ...
- css布局之负margin妙用及其他实现
相信大家在项目的开发中都遇到过这样的需求,一行放X(X>1)个块且相邻块之间的间距相同. 大概就是上面这个样子,下面介绍几种实现的方式. 1.负margin大法 设置好元素的宽度和留白占满父级的 ...
- javascript获取CSS3浏览器前缀
var prefix = (function () { var styles = window.getComputedStyle(document.documentElement, ''), pre ...
- 秒味课堂Angular js笔记------Angular js中的工具方法
Angular js中的工具方法 angular.isArray angular.isDate angular.isDefined angular.isUndefined angular.isFunc ...