http://vjudge.net/contest/view.action?cid=48477#problem/A
Description
As you must have experienced, instead of landing immediately, an aircraft sometimes waits in a holding loop close to the runway. This holding mechanism is required by air traffic controllers to space apart aircraft as much as possible on the runway (while keeping delays low). It is formally defined as a ``holding pattern'' and is a predetermined maneuver designed to keep an aircraft within a specified airspace (see Figure 1 for an example).
Jim Tarjan, an air-traffic controller, has asked his brother Robert to help him to improve the behavior of the airport.
Input
The input file, that contains all the relevant data, contains several test cases
Each test case is described in the following way. The first line contains the number n of aircraft ( 2n2000). This line is followed byn lines. Each of these lines contains two integers, which represent the early landing time and the late landing time of an aircraft. Note that all times t are such that 0t107.
Output
For each input case, your program has to write a line that conttains the maximal security gap between consecutive landings.
Sample Input
10 44 156 153 182 48 109 160 201 55 186 54 207 55 165 17 58 132 160 87 197
Sample Output
10
有n架飞机需要着陆。每架飞机都可以选择“早着陆”或者是晚着陆两种方式之一。且必须选择一种。第i架飞机最早的着陆时间为Ei 最晚的着陆时间为Li,不得在其他时间着陆。你的任务是为这些飞机安排着陆的方式,使得整个着陆计划计划尽量安全,换句话说,如果把所有的飞机的实际着陆时间按照从早到晚的顺序排序,相邻的两个着陆时间间隔的最小值应尽量最大。
简单思路:(摘自大白书)
“最小值应该最大”的典型处理方式是用二分的方法查找最佳的答案P,这样原来的问题转化为了判定问题“是否存在一个调度方案,使得相邻的两个着陆时间差总是不小于P”,而这个问题可以进一步转化为:任意两个着陆时间差总不小于P。令布尔变量xi表示第i架飞机是否早着陆,则唯一的限制就是"时间差不小于P的两个着陆时间不能同时满足"。
#include <cstdio> #include <iostream> #include <string.h> #include <cmath> using namespace std; const int maxn=2025; struct note { int to; int next; }; struct twosat { int head[maxn*2]; note edge[maxn*2*maxn*2]; int ip,n; bool mark[maxn*2]; int S[maxn*2],c; void init(int n) { this->n=n; memset(mark,0,sizeof(mark)); memset(head,-1,sizeof(head)); ip=0; } void addedge(int u,int v) { edge[ip].to=v,edge[ip].next=head[u],head[u]=ip++; } // x=xval or y=yval void add_cluse(int x,int xval,int y,int yval) { x=x*2+xval; y=y*2+yval; addedge(x^1,y); addedge(y^1,x); } //x=xval /*void add_con(int x,int xval) { x=x*2+xval; addedge(x^1,x); }*/ bool dfs(int x) { if(mark[x^1]) return false; if(mark[x]) return true; mark[x]=true; S[c++]=x; for(int i=head[x]; i!=-1; i=edge[i].next) if(!dfs(edge[i].to)) return false; return true; } bool solve() { for(int i=0; i<n*2; i+=2) if(!mark[i]&&!mark[i+1]) { c=0; if(!dfs(i)) { while(c>0) mark[S[--c]]=false; if(!dfs(i+1)) return false; } } return true; } }; twosat solver; int n,T[maxn][2]; bool test(int diff) { solver.init(n); for(int i=0; i<n; i++) for(int a=0; a<2; a++) for(int j=i+1; j<n; j++) for(int b=0; b<2; b++) if(abs(T[i][a]-T[j][b])<diff) solver.add_cluse(i,a^1,j,b^1); return solver.solve(); } int main() { while(~scanf("%d",&n)) { if(n==0) break; int L=0,R=0; for(int i=0; i<n; i++) for(int a=0; a<2; a++) { scanf("%d",&T[i][a]); R=max(R,T[i][a]); } while(L<R) { int M=(L+R+1)/2; if(test(M)) L=M; else R=M-1; } printf("%d\n",L); } return 0; }