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.
The Terminal Radar Approach CONtrol (TRACON) controls aircraft approaching and departing when they are between 5 and 50 miles of the airport. In this final scheduling process, air traffic controllers make some aircraft wait before landing. Unfortunately this ``waiting'' process is complex as aircraft follow predetermined routes and their speed cannot be changed. To reach some degree of flexibility in the process, the basic delaying procedure is to make aircraft follow a holding pattern that has been designed for the TRACON area. Such patterns generate a constant prescribed delay for an aircraft (see Figure 1 for an example). Several holding patterns may exist in the same TRACON.
In the following, we assume that there is a single runway and that when an aircraft enters the TRACON area, it is assigned an early landing time, a late landing time and a possible holding pattern. The early landing time corresponds to the situation where the aircraft does not wait and lands as soon as possible. The late landing time corresponds to the situation where the aircraft waits in the prescribed holding pattern and then lands at that time. We assume that an aircraft enters at most one holding pattern. Hence, the early and late landing times are the only two possible times for the landing.
The security gap is the minimal elapsed time between consecutive landings. The objective is to maximize the security gap. Robert believes that you can help.
Assume there are 10 aircraft in the TRACON area. Table 1 provides the corresponding early and late landing times (columns ``Early'' and ``Late'').
Aircraft | Early | Late | Solution |
A1 | 44 | 156 | Early |
A2 | 153 | 182 | Early |
A3 | 48 | 109 | Late |
A4 | 160 | 201 | Late |
A5 | 55 | 186 | Late |
A6 | 54 | 207 | Early |
A7 | 55 | 165 | Late |
A8 | 17 | 58 | Early |
A9 | 132 | 160 | Early |
A10 | 87 | 197 | Early |
The maximal security gap is 10 and the corresponding solution is reported in Table 1 (column ``Solution''). In this solution, the aircraft land in the following order: A8, A1, A6, A10, A3, A9, A2, A7, A5, A4. The security gap is realized by aircraft A1 and A6.
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 by n 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.
For each input case, your program has to write a line that conttains the maximal security gap between consecutive landings.
10 44 156 153 182 48 109 160 201 55 186 54 207 55 165 17 58 132 160 87 197
10
Note: The input file corresponds to Table 1.
Robert's Hints
And now comes Robert's big insight: our problem has a solution, if and only if we have no contradiction. A contradiction being something like Ai ¬Ai.
题目大意:有 n 架飞机,每架飞机都有两个着陆时间,一个是早着陆时间 E ,另一个是晚着陆时间 L 。每架飞机只能选择一种方式着陆,并且两架飞机不能在同一时间降落。两个相邻的着陆时间 的差值的最小值成为安全间隙时间。现在请你安排每架飞机的着陆方式使得安全间隙时间最大。
解题思路:找安全间隙时间的最大值可以用二分的方式。对于一架飞机来说,要么选择早着陆,要么选择晚着陆,相当于对于一个条件 X , X 要么为真,要么为假,这就符合了2 - SAT 的基本特征 ,假设第i架飞机早着陆 为真 用 X2i 表示,晚着陆 为真 用X2i+1 表示,下面说一下如何建图:
1、对于一个安全间隙时间 t ,举个例子:如果 Ei - Ej < t (即 不能同时安排 第i架飞机早着陆 和 第j架飞机早着陆),即X2i 与 X2j不能同时为真, 即条件为 !(X2i && X2j) <==> (!X2i|| !X2j),这就满足了2 - SAT 的条件。
2、要满足上面的条件,连边 2i -> 2j + 1 和 2j -> 2i + 1 即可。
Ps:2 - SAT 问题的建图要想明白,还有就是注意 位运算 的灵活运用。
代码如下:
#include<iostream> #include<cstring> #include<string> #include<algorithm> #include<vector> #include<cmath> #include<cstdio> #include<queue> #define mem(a , b) memset(a , b , sizeof(a)) using namespace std ; const int MAXN = 2005 ; int tm[MAXN * 2] ; // 存放时间 bool mark[MAXN * 2] ; vector<int> G[MAXN * 2] ; int S[MAXN * 2] , c ; // 模拟栈 int n ; int le , r , mid ; void init() // 输入 { r = 0 ; int i ; for(i = 0 ; i < n ; i ++) { int a , b ; scanf("%d%d" , &a , &b) ; tm[i << 1] = a ; // tm[i * 2] 代表第 i 架飞机的早着陆时间 r = max(a , r) ; tm[(i << 1) + 1] = b ; // tm[i * 2 + 1] 代表 第 i 架飞机的晚着陆时间 r = max(b , r) ; } } void chu() // 清空图 { int i ; for(i = 0 ; i < n * 2 ; i ++) { G[i].clear() ; } } void add_e(int x , int y) // 向图中添加边 { G[x].push_back(y ^ 1) ; G[y].push_back(x ^ 1) ; } void build_G(int t) // 建图 { chu() ; int i , j ; for(i = 0 ; i < n * 2 ; i ++) { for(j = i + 1 ; j < n * 2 ; j ++) { if(j == (i ^ 1) ) continue ; if(abs(tm[i] - tm[j]) < t) { add_e(i , j) ; } } } } bool dfs(int x) { if(mark[x ^ 1]) return false ; if(mark[x]) return true ; mark[x] = true ; S[c ++] = x ; int i ; for(i = 0 ; i < G[x].size() ; i ++) { if(!dfs(G[x][i])) return false ; } return true ; } int can(int t) { build_G(t) ; mem(mark , 0) ; int i ; for(i = 0 ; i < n ; i ++) { if(!mark[i * 2] && !mark[i * 2 + 1]) { c = 0 ; if(!dfs(i * 2)) { while (c > 0) { mark[S[-- c]] = false ; } if(!dfs(i * 2 + 1)) { return 0 ; } } } } return 1 ; } void solve() { le = 0 ; while (le < r) { mid = le + (r - le + 1) / 2 ; // 注意此处求 mid 的方法,这是二分的一个技巧!! if(can(mid)) { le = mid ; } else { r = mid - 1 ; } } printf("%d\n" , le) ; } int main() { while (scanf("%d" , &n) != EOF) { init() ; solve() ; } return 0 ; }