2109&2535: [Noi2010]Plane 航空管制 - BZOJ
Description
世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生。最近,小X就因为航空管制,连续两次在机场被延误超过了两小时。对此,小X表示很不满意。 在这次来烟台的路上,小 X不幸又一次碰上了航空管制。于是小 X开始思考关于航空管制的问题。 假设目前被延误航班共有 n个,编号为 1至n。机场只有一条起飞跑道,所有的航班需按某个顺序依次起飞(称这个顺序为起飞序列)。定义一个航班的起飞序号为该航班在起飞序列中的位置,即是第几个起飞的航班。 起飞序列还存在两类限制条件: 第一类(最晚起飞时间限制):编号为 i的航班起飞序号不得超过 ki; 第二类(相对起飞顺序限制):存在一些相对起飞顺序限制(a, b),表示航班 a的起飞时间必须早于航班 b,即航班 a的起飞序号必须小于航班 b 的起飞序号。 小X 思考的第一个问题是,若给定以上两类限制条件,是否可以计算出一个可行的起飞序列。第二个问题则是,在考虑两类限制条件的情况下,如何求出每个航班在所有可行的起飞序列中的最小起飞序号。
Input
第一行包含两个正整数 n和m,n表示航班数目,m表示第二类限制条件(相对起飞顺序限制)的数目。 第二行包含 n个正整数 k1, k2, „, kn。 接下来 m行,每行两个正整数 a和b,表示一对相对起飞顺序限制(a, b),其中1≤a,b≤n, 表示航班 a必须先于航班 b起飞。
Output
由两行组成。
第一行包含 n个整数,表示一个可行的起飞序列,相邻两个整数用空格分隔。
输入数据保证至少存在一个可行的起飞序列。如果存在多个可行的方案,输出任
意一个即可。
第二行包含 n个整数 t1, t2, „, tn,其中 ti表示航班i可能的最小起飞序
号,相邻两个整数用空格分隔。
Sample Input
5 5
4 5 2 5 4
1 2
3 2
5 1
3 4
3 1
Sample Output
3 5 1 4 2
3 4 1 2 1
orz VFK
首先把边反过来,k[i]变成n-k[i],整个序列反过来
就变成另一个问题了,就是点i的序号必须大于k[i],还有许多关系(a,b)表示a必须在b前面出现
然后要求一个可行序列和每个点最晚出现的时间
求可行序列就直接拓扑排序
求最晚出现时间就枚举点i
我们先把点i放一边不去管它,然后拓扑排序直到不能排为止(入度为0的点(不包括i)都不能在这个时间出现),这个就是最晚时间了
我懒,用堆写的,实际是过不去的,bzoj总时限可以过
const
maxn=;
maxm=;
var
k,d,first:array[..maxn]of longint;
last,next:array[..maxm]of longint;
n,m,tot:longint; procedure insert(x,y:longint);
begin
inc(tot);
last[tot]:=y;
next[tot]:=first[x];
first[x]:=tot;
inc(d[y]);
end; var
q,kk,du:array[..maxn]of longint;
r:longint; procedure swap(var x,y:longint);
var
t:longint;
begin
t:=x;x:=y;y:=t;
end; procedure up(x:longint);
var
i:longint;
begin
while x> do
begin
i:=x>>;
if kk[q[x]]>kk[q[i]] then
begin
swap(q[i],q[x]);
x:=i;
end
else exit;
end;
end; procedure down(x:longint);
var
i:longint;
begin
i:=x<<;
while i<=r do
begin
if (i<r) and (kk[q[i+]]>kk[q[i]]) then inc(i);
if kk[q[i]]>kk[q[x]] then
begin
swap(q[i],q[x]);
x:=i;i:=x<<;
end
else exit;
end;
end; procedure delete;
begin
swap(q[],q[r]);
dec(r);down();
end; function time(x:longint):longint;
var
i:longint;
begin
time:=n;r:=;
for i:= to n do du[i]:=d[i];
for i:= to n do kk[i]:=k[i];
for i:= to n do
if (du[i]=) and (i<>x) then
begin
inc(r);q[r]:=i;
up(r);
end;
while r> do
begin
if time>kk[q[]] then break;
kk[q[]]:=n+;
dec(time);
i:=first[q[]];
while i<> do
begin
dec(du[last[i]]);
if (du[last[i]]=) and (last[i]<>x) then
begin
inc(r);q[r]:=last[i];
up(r);
end;
i:=next[i];
end;
delete;
end;
if du[x]<> then exit(-);
end; var
ans:array[..maxn]of longint; procedure work;
var
i,cnt:longint;
begin
r:=;cnt:=n;
for i:= to n do kk[i]:=k[i];
for i:= to n do du[i]:=d[i];
for i:= to n do
if du[i]= then
begin
inc(r);q[r]:=i;
up(r);
end;
while r> do
begin
ans[cnt]:=q[];
dec(cnt);
i:=first[q[]];
kk[q[]]:=n+;
while i<> do
begin
dec(du[last[i]]);
if du[last[i]]= then
begin
inc(r);q[r]:=last[i];
up(r);
end;
i:=next[i];
end;
delete;
end;
for i:= to n do
if i<n then write(ans[i],' ')
else writeln(ans[i]);
end; procedure main;
var
i,x,y:longint;
begin
read(n,m);
for i:= to n do read(k[i]);
for i:= to m do
begin
read(x,y);
insert(y,x);
end;
work;
for i:= to n do
if i<n then write(time(i),' ')
else write(time(i));
end; begin
main;
end.
后来懂了用链表写,听起来蛮简单就写了一个
const
maxn=;
maxm=;
var
first,k,d,di,head,suc,ans:array[..maxn]of longint;
last,next:array[..maxm]of longint;
n,m,tot:longint; procedure insert(x,y:longint);
begin
inc(tot);
last[tot]:=y;
next[tot]:=first[x];
first[x]:=tot;
inc(d[y]);
end; procedure merge(x,y:longint);
begin
if head[x]= then
begin
head[x]:=head[y];
exit;
end;
x:=head[x];
while suc[x]<> do x:=suc[x];
suc[x]:=head[y];
end; function time(x:longint):longint;
var
i,v:longint;
begin
for i:= to n do di[i]:=d[i];
for i:= to n do head[i]:=;
for i:= to n do
if (d[i]=) and (i<>x) then
begin
suc[i]:=head[k[i]];
head[k[i]]:=i;
end;
time:=n;
while true do
begin
if head[time]= then exit;
i:=first[head[time]];
head[time]:=suc[head[time]];
while i<> do
begin
dec(di[last[i]]);
if (di[last[i]]=) and (last[i]<>x) then
begin
if k[last[i]]<time then v:=k[last[i]]
else v:=time;
suc[last[i]]:=head[v];
head[v]:=last[i];
end;
i:=next[i];
end;
merge(time-,time);
dec(time);
end;
end; procedure work;
var
i,t,v:longint;
begin
for i:= to n do di[i]:=d[i];
for i:= to n do head[i]:=;
for i:= to n do
if d[i]= then
begin
suc[i]:=head[k[i]];
head[k[i]]:=i;
end;
t:=n;
while t> do
begin
ans[t]:=head[t];
i:=first[head[t]];
head[t]:=suc[head[t]];
while i<> do
begin
dec(di[last[i]]);
if di[last[i]]= then
begin
if k[last[i]]<t then v:=k[last[i]]
else v:=t;
suc[last[i]]:=head[v];
head[v]:=last[i];
end;
i:=next[i];
end;
merge(t-,t);
dec(t);
end;
end; procedure main;
var
i,x,y:longint;
begin
read(n,m);
for i:= to n do read(k[i]);
for i:= to m do
begin
read(x,y);
insert(y,x);
end;
work;
for i:= to n- do write(ans[i],' ');
writeln(ans[n]);
for i:= to n- do write(time(i),' ');
write(time(n));
end; begin
main;
end.
2109&2535: [Noi2010]Plane 航空管制 - BZOJ的更多相关文章
- BZOJ 2535: [Noi2010]Plane 航空管制2
Description 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上 ...
- bzoj 2535: [Noi2010]Plane 航空管制2【拓扑排序+堆】
有个容易混的概念就是第一问的答案不是k[i]字典序最小即可,是要求k[i]大的尽量靠后,因为这里前面选的时候是对后面有影响的(比如两条链a->b c->d,ka=4,kb=2,kc=3,k ...
- bzoj 2109: [Noi2010]Plane 航空管制
Description 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频 发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此, 小X表示很不满意. 在这次来烟台的 ...
- BZOJ2109: [Noi2010]Plane 航空管制
Description 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频 发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此, 小X表示很不满意. 在这次来烟台的 ...
- BZOJ2535 [Noi2010]Plane 航空管制2
Description 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上 ...
- bzoj 2535 && bzoj 2109 [Noi2010]Plane 航空管制——贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2535 https://www.lydsy.com/JudgeOnline/problem.p ...
- BZOJ2535: [Noi2010]Plane 航空管制2(拓扑排序 贪心)
题意 题目链接 Sol 非常妙的一道题. 首先不难想到拓扑排序,但是直接对原图按\(k\)从小到大拓扑排序是错的.因为当前的\(k\)大并不意味着后面的点\(k\)也大 但是在反图上按\(k\)从大到 ...
- BZOJ2535 [Noi2010]Plane 航空管制 【贪心 + 堆】
题目链接 BZOJ2535 题解 航班之间的关系形成了一个拓扑图 而且航班若要合法,应尽量早出发 所以我们逆拓扑序选点,能在后面出发的尽量后面出发,不会使其它点变得更劣,容易知是正确的 第二问只需枚举 ...
- 【BZOJ2109/2535】【NOI2010】航空管制(贪心)
[BZOJ2109/2535][NOI2010]航空管制(贪心) 题面 BZOJ2109 BZOJ2535 题解 很好玩的一道题目 先看第一问,显然是要找一个合法的拓扑排序的序列. 直接拓扑排序,把队 ...
随机推荐
- raphael画图
// 在坐标(10,50)创建宽320,高200的画布 var paper = Raphael(10, 50, 320, 200); // 在坐标(x = 50, y = 40)绘制半径为 10 的圆 ...
- Mac下利用eclipse打包cocos2dx安卓apk
上一篇博客中我们已经配置好了JDK和ANT,接下来我们就可以使用eclipse将我们开发好的cocos2dx项目进行打包了. 首先,我们需要准备好下面的内容 配置环境变量,打开"终端&quo ...
- Firebug 调试技巧之console API
console.log(object[, object, ...]) Writes a message to the console. You may pass as many arguments a ...
- "Could not load file or assembly 'DTcms.Web.UI' or one of its dependencies. 拒绝访问。" 的解决办法
出现的问题提示如下:
- 20141009---Visual Studio 2012 预定义数据类型
预定义数据类型 一.值类型 整型:(整数) 有符号整型和无符号整形,区别是有符号的有负数无符号的都是正数, 2x+1 常用int 有符号: 带有正负数,范围为按所写依次增大 ...
- Linux开机启动程序详解[转]
Linux开机启动程序详解 我们假设大家已经熟悉其它操作系统的引导过程,了解硬件的自检引导步骤,就只从Linux操作系统的引导加载程序(对个人电脑而言通常是LILO)开始,介绍Linux开机引导的步骤 ...
- (转)Yale CAS + .net Client 实现 SSO(2)
第一部分:安装配置 Tomcat 第二部分:安装配置 CAS 1. 下载 CAS 及.NET CAS client. CAS下载地址:http://www.jasig.org/cas/download ...
- Rhythmbox中文乱码解决办法
今天在网络上找到了一个比较好的解决Rhythmbox中文乱码的问题的方法 进入你的音乐文件夹执行如下代码: mid3iconv -e GBK *.mp3 如果没有提示多试几次,有可能系统会提示: py ...
- MySQL触发器 Update触发Insert失败
今天工作需要,想要实现将仅对状态更新的表进行历史记录显示,于是考虑在原表中建立触发器,将更新的内容同时写入另一张表 于是进行测试 --建立测试表CREATE TABLE `triggletest_tr ...
- struts2 type="redirect"源码解析
首先解释一下几个名词: request.getRequestDispatcher()是请求转发,前后页面共享一个request ; response.sendRedirect()是重新定向,前后页面不 ...