Now or later(2 - sat + 二分)

3211 - Now or later

Time limit: 9.000 seconds

 

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).

 

Figure 1: A simple Holding Pattern as described in a pilot text book.

 

 

 

\epsfbox{p3211.eps}

Jim Tarjan, an air-traffic controller, has asked his brother Robert to help him to improve the behavior of the airport.

 

The TRACON area 

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.

 

Example 

Assume there are 10 aircraft in the TRACON area. Table 1 provides the corresponding early and late landing times (columns ``Early'' and ``Late'').

 

 

Table 1: A 10 aircraft instance of the problem.

 

 

 

AircraftEarlyLateSolution
A144156Early
A2153182Early
A348109Late
A4160201Late
A555186Late
A654207Early
A755165Late
A81758Early
A9132160Early
A1087197Early

 


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: A8A1A6A10A3A9A2A7A5A4. The security gap is realized by aircraft A1 and A6.

 

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 ( 2$ \le$n$ \le$2000). This line is followed by nlines. 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 0$ \le$t$ \le$107.

 

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

 

 


Note: The input file corresponds to Table 1.

 

 


Robert's Hints

 

Optimization vs. Decision
Robert advises you to work on the decision variant of the problem. It can then be stated as follows: Given an integer  p, and an instance of the optimization problem, the question is to decide if there is a solution with security gap  p or not. Note that, if you know how to solve the decision variant of an optimization problem, you can build a binary search algorithm to find the optimal solution.

 

On decision
Robert believes that the decision variant of the problem can be  modeled as a very particular boolean satisfaction problem. Robert suggests to associate a boolean variable per aircraft stating whether the aircraft is early (variable takes value ``true'') or late (value ``false''). It should then be easy to see that for some aircraft to land at some time has consequences for the landing times of other aircraft. For instance in Table 1 and with a delay of 10, if aircraft  A 1 lands early, then aircraft  A 3 has to land late. And of course, if aircraft  A 3 lands early, then aircraft  A 1 has to land late. That is, aircraft  A 1 and  A 3 cannot both land early and formula ( A 1  $ \Rightarrow$ ¬ A 3$ \wedge$ ( A 3  $ \Rightarrow$ ¬ A 1) must hold.

 


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 $ \Leftrightarrow$ ¬Ai.

 

         题意:

         给出 N (2 ~ 2000)个飞机降落时间,一架飞机要不在早时间点 S(0 ~ 10 ^ 7) 降落,要不在晚时间点 E(0 ~ 10 ^ 7) 降落。现要求飞机降落时间间隔最小值最大化。

 

         思路:

         2 - sat + 二分。枚举时间间隔 T,若两个时间点间隔 t < T,则可以确定一种确定关系,两者选其一。每二分结果一次就建图一次,区间左闭右开,最后左区间值即为所求。

 

         AC:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <cmath>
#include <vector>

using namespace std;

const int NMAX = 2005 * 2;
const int INF = 10000005;

int n;
int Sta[NMAX], End[NMAX];

vector<int> G[NMAX];

int scc_cnt, dfs_clock;
int pre[NMAX], low[NMAX], cmp[NMAX];
stack<int> s;

void dfs(int u) {
        pre[u] = low[u] = ++dfs_clock;
        s.push(u);

        for (int i = 0; i < G[u].size(); ++i) {
                int v = G[u][i];
                if (!pre[v]) {
                        dfs(v);
                        low[u] = min(low[u], low[v]);
                } else if (!cmp[v]) {
                        low[u] = min(low[u], pre[v]);
                }
        }

        if (low[u] == pre[u]) {
                ++scc_cnt;
                for(;;) {
                        int x = s.top(); s.pop();
                        cmp[x] = scc_cnt;
                        if (x == u) break;
                }
        }
}

void scc() {
        dfs_clock = scc_cnt = 0;
        memset(cmp, 0, sizeof(cmp));
        memset(pre, 0, sizeof(pre));

        for (int i = 1; i <= 2 * n; ++i) {
                if (!pre[i]) dfs(i);
        }
}

bool C(int d) {
        for (int i = 1; i <= 2 * n; ++i)
                G[i].clear();

        for (int i = 1; i <= n - 1; ++i) {
                for (int j = i + 1; j <= n; ++j) {
                        if (abs(Sta[i] - Sta[j]) < d) {
                                G[i].push_back(j + n);
                                G[j].push_back(i + n);
                        }
                        if (abs(Sta[i] - End[j]) < d) {
                                G[i].push_back(j);
                                G[n + j].push_back(n + i);
                        }
                        if (abs(End[i] - Sta[j]) < d) {
                                G[n + i].push_back(n + j);
                                G[j].push_back(i);
                        }
                        if (abs(End[i] - End[j]) < d) {
                                G[n + i].push_back(j);
                                G[n + j].push_back(i);
                        }
                }
        }

        scc();

        for (int i = 1; i <= n; ++i)
                if (cmp[i] == cmp[i + n]) return false;

        return true;
}

void solve() {
        int l = 0, r = INF;

        while (r - l > 1) {
                int mid = l + (r - l) / 2;
                if (C(mid)) l = mid;
                else r = mid;
        }

        printf("%d\n", l);
}

int main() {

        while (~scanf("%d", &n)) {
                for (int i = 1; i <= n; ++i)
                        scanf("%d%d", &Sta[i], &End[i]);

                solve();

        }

        return 0;
}

 

 

 

        

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值