Now or later
64-bit integer IO format: %lld Java class name: Main
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 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.
Output
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.
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
Source
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 5010; 4 struct arc { 5 int to,next; 6 arc(int x = 0,int y = -1) { 7 to = x; 8 next = y; 9 } 10 }; 11 arc e[maxn*maxn]; 12 int head[maxn],dfn[maxn],low[maxn],belong[maxn]; 13 int tot,cnt,scc,n,m; 14 bool instack[maxn]; 15 stack<int>stk; 16 void add(int u,int v) { 17 e[tot] = arc(v,head[u]); 18 head[u] = tot++; 19 } 20 void init() { 21 for(int i = 0; i < maxn; i++) { 22 belong[i] = 0; 23 low[i] = dfn[i] = 0; 24 head[i] = -1; 25 instack[i] = false; 26 } 27 while(!stk.empty()) stk.pop(); 28 tot = cnt = scc = 0; 29 } 30 void tarjan(int u) { 31 dfn[u] = low[u] = ++cnt; 32 instack[u] = true; 33 stk.push(u); 34 for(int i = head[u]; ~i; i = e[i].next) { 35 if(!dfn[e[i].to]) { 36 tarjan(e[i].to); 37 if(low[e[i].to] < low[u]) low[u] = low[e[i].to]; 38 } else if(instack[e[i].to] && dfn[e[i].to] < low[u]) 39 low[u] = dfn[e[i].to]; 40 } 41 if(dfn[u] == low[u]) { 42 scc++; 43 int v; 44 do { 45 v = stk.top(); 46 stk.pop(); 47 instack[v] = false; 48 belong[v] = scc; 49 } while(v != u); 50 } 51 } 52 bool solve() { 53 for(int i = 0; i < (n<<1); i++) 54 if(!dfn[i]) tarjan(i); 55 for(int i = 0; i < n; i++) 56 if(belong[i] == belong[i+n]) return false; 57 return true; 58 } 59 int tim[maxn][2]; 60 bool check(int t) { 61 init(); 62 for(int i = 0; i < n; ++i) 63 for(int j = i+1; j < n; ++j) { 64 if(abs(tim[i][0] - tim[j][0]) < t) { 65 add(i,j+n); 66 add(j,i+n); 67 } 68 if(abs(tim[i][0] - tim[j][1]) < t) { 69 add(i,j); 70 add(j+n,i+n); 71 } 72 if(abs(tim[i][1] - tim[j][0]) < t) { 73 add(i+n,j+n); 74 add(j,i); 75 } 76 if(abs(tim[i][1] - tim[j][1]) < t) { 77 add(i+n,j); 78 add(j+n,i); 79 } 80 } 81 for(int i = 0; i < 2*n; ++i) 82 if(!dfn[i]) tarjan(i); 83 for(int i = 0; i < n; ++i) 84 if(belong[i] == belong[i+n]) return false; 85 return true; 86 } 87 int main() { 88 while(~scanf("%d",&n)) { 89 int low = 0,high = 0; 90 for(int i = 0; i < n; ++i) { 91 for(int j = 0; j < 2; ++j) { 92 scanf("%d",tim[i]+j); 93 high = max(high,tim[i][j]); 94 } 95 } 96 int ret = -1; 97 while(low <= high) { 98 int mid = (low + high)>>1; 99 if(check(mid)) { 100 ret = mid; 101 low = mid+1; 102 } else high = mid-1; 103 } 104 printf("%d ",ret); 105 } 106 return 0; 107 }