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 航空管制的更多相关文章

  1. NOI2010航空管制

    2008: [Noi2010]航空管制 Time Limit: 10 Sec  Memory Limit: 552 MBSubmit: 31  Solved: 0[Submit][Status] De ...

  2. [BZOJ2109][NOI2010]航空管制(贪心+拓扑)

    2109: [Noi2010]Plane 航空管制 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1227  Solved: 510[Submit][ ...

  3. bzoj2535 [Noi2010]航空管制

    Description 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上 ...

  4. [NOI2010]航空管制(拓扑排序+贪心)

    题目描述 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上,小X不幸又一 ...

  5. BZOJ.2109.[NOI2010]航空管制(拓扑 贪心)

    题目链接 双倍经验(没有第一问) \(Description\) \(Solution\) 第一问拓扑排序即可. 第二问,即让一个元素在拓扑序中尽量靠前,好像不好做. 但是可以让一个元素出现尽量靠后. ...

  6. 【[NOI2010]航空管制】

    关于拓扑排序的反建图还是一个非常套路的东西 比如说[HNOI2015]菜肴制作 我们希望使得某一个东西在拓扑序中出现的尽可能早,这个时候就可以建出一张反图来,使得这个东西在反图中的拓扑序尽量靠后,从而 ...

  7. 洛谷 P1954 [NOI2010]航空管制

    https://www.luogu.org/problemnew/show/P1954 拓扑排序, 注意到如果正着建图("a出现早于b"=>"a向b连边" ...

  8. BZOJ 2535:NOI 2010 航空管制

    [NOI2010]航空管制 题面请点上面. 首先第一问,我第一想法是把它放到一个小根堆中,然而这是不行的. 正确的思路是,把图反过来建,然后放到一个大根堆里去. 至于原因,感性理解一下,正着贪是有后效 ...

  9. 【BZOJ2109/2535】【NOI2010】航空管制(贪心)

    [BZOJ2109/2535][NOI2010]航空管制(贪心) 题面 BZOJ2109 BZOJ2535 题解 很好玩的一道题目 先看第一问,显然是要找一个合法的拓扑排序的序列. 直接拓扑排序,把队 ...

随机推荐

  1. Android控件Editext、TextView属性详解

    EditText属性描述 android:layout_gravity="center_vertical"//设置控件显示的位置:默认top,这里居中显示,还有bottom and ...

  2. 提高VS2010/VS2012编译速度

    除了合理的划分模块,减少link的时间外,充分利用多核编译也很重要. VS2010/2012都可以用多核编译,需要同时设置如下两个参数: Enable Minimal Rebuild  Propert ...

  3. jQuery 對table的基本操作

    一.鼠标移动到行更换背景色: css样式: .hover{ background-color: #cccc00; } Js脚本: $(document).ready(function () { //鼠 ...

  4. Java基础知识强化79:被遗忘的Java Math类

    1. Math类概述 Math类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. 2. 成员变量 和 成员方法(常用的) (1)成员变量 public static final d ...

  5. 线段树---HDU1394Minimum Inversion Number

    此题和上题略有不同,但是大体差不多,不过要把题意转换过来,题目大体意思为, 输入n, 也就是n个数,这些数为0 - (n-1), 这些数刚开始给定输入的顺序, 然后求他的逆序数,然后接着把第一个移到这 ...

  6. 计算方法(一)用C#实现数值迭代

    平时,经常会遇到解方程,计算方法中常用的有二分法(精度太低,迭代次数多,一般没人用),牛顿迭代法,弦截法,网上大多都是C++或者Java的实现代码,很少有C#的,我在本科毕业论文中用到了这些,那时也需 ...

  7. Maven 镜像

    http://mvnrepository.com/http://search.maven.org/http://repository.sonatype.org/content/groups/publi ...

  8. JDK1.5新特性随手记

    1.静态导入 import static 静态导入前写法: public class TestStatic { public static void main(String[] args) { Sys ...

  9. vi初接触

    vi初接触 它有三种模式: 一 一般模式 二 编辑模式 三 命令行模式 介绍几种比较常用的吧 -- 退出:q 写入:w 强制:! (以上可叠加) 显示行号:set nu 取消:set nonu 跳转到 ...

  10. CentOS下几种软件安装方式

    1.rpmRPM RedHat Package Manager(RedHat软件包管理工具)的缩写,这一文件格式名称虽然打上了RedHat的标志, 但是其原始设计理念是开放式的,现在包括OpenLin ...