9-21 2-SAT,贪心,二分

Now or later

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.
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”).
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
Table 1: A 10 aircraft instance of the problem.
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.

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 ≤ n ≤ 2000). 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 0 ≤ t ≤ 107
.

Output

For each input case, your program has to write a line that conttains the maximal security gap between
consecutive landings.
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 A1
lands early, then aircraft A3 has to land late. And of course, if aircraft A3 lands early, then
aircraft A1 has to land late. That is, aircraft A1 and A3 cannot both land early and formula
(A1 ⇒ ¬A3) ∧ (A3 ⇒ ¬A1) 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 ⇔ ¬Ai
.

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,不得在其他时间着陆。你的任务是为这些飞机安排着陆方式,使得整个着陆计划尽量安全。换言之,如果把所有飞机的实际着陆时间从小到大排序,相邻两个着陆时间间隔的最小值应尽量大。

先用的二分+贪心的做法,理所当然的T了
然后乖乖用2-sat+二分, 在加边那一块需要加深理解, 这个复杂度是O(n*n*logT), 注意熟练一下计算方法

2-sat+二分

自己AC, 请与刘汝佳的代码比较一下,,自己写的有多丑。。

using namespace std;
int f[2010][2];
bool mark[4020]; //注意大小
vector<int> g[4020], path;
int n;

void add_edge(int i, int x, int j, int y){
    i = i*2 + x;
    j = j*2 + y;
    g[i^1].push_back(j);
    g[j^1].push_back(i);
}

void init(int mid){
    memset(mark, 0, sizeof(mark)); path.clear();
    for(int i = 0; i < 2*n; i++) g[i].clear();
    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(f[i][a] - f[j][b]) < mid) add_edge(i, a^1, j, b^1);
}

bool dfs(int pos){
    if(mark[pos^1]) return false;
    if(mark[pos]) return true;
    mark[pos] = true;
    path.push_back(pos);
    for(int i = 0; i < g[pos].size(); i++){
        if(!dfs(g[pos][i])) return false;
    }
    return true;
}

bool check(int mid){
    init(mid);
    for(int i = 0; i < 2*n; i+=2){
        if(!mark[i] && !mark[i+1]){
            path.clear();
            if(!dfs(i)){
                for(int j = 0; j < path.size(); j++)
                    mark[path[j]] = false;
                path.clear();
                if(!dfs(i+1)){return false;}
            }
        }
    }
    return true;
}



int main()
{
    //freopen("1.txt", "r", stdin);
    while(~scanf("%d", &n)){
        for(int i = 0; i < n; i++)
            scanf("%d%d", &f[i][0], &f[i][1]);

        int l = 0, r = 1e7, mid;
        while(r > l){
            mid = l + (r - l + 1)/2;
            if(check(mid)) l = mid;
            else    r = mid - 1;
        }
        printf("%d\n", l);
    }
    return 0;
}

刘汝佳

//刘汝佳
#include <bits/stdc++.h>
using namespace std;
int n;
const int maxn = 2010;
int gra[maxn][2];


struct Two_SAT{
    int n;
    vector<int> G[maxn*2];
    bool mark[maxn*2];
    int s[maxn*2], c;

    bool dfs(int x){
        if(mark[x^1]) return false;
        if(mark[x]) return true;
        mark[x] = true;
        s[c++] = x;
        for(int i = 0; i < G[x].size(); i++)
            if(!dfs(G[x][i])) return false;
        return true;
    }

    void init(int n){
        this->n = n;
        for(int i = 0; i < n*2; i++) G[i].clear();
        memset(mark, 0, sizeof(mark));
    }

    void add_clause(int x, int xval, int y, int yval){
        x = x*2+xval;
        y = y*2+yval;
        G[x^1].push_back(y);
        G[y^1].push_back(x);
    }

    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;
    }

}cas;

bool test(int mid){
    cas.init(n);
    for(int i = 0; i < n; i++) for(int s = 0; s < 2; s++)
        for(int j = i+1; j < n; j++) for(int t = 0; t < 2; t++)
            if(abs(gra[i][s] - gra[j][t]) < mid) cas.add_clause(i, s^1, j, t^1);
    return cas.solve();
}

int main()
{
    while(~scanf("%d", &n)){
        int l = 0, r = 0;
        for(int i = 0; i < n; i++) for(int a = 0; a < 2; a++){
            scanf("%d%d", &gra[i][a]);
            r = max(gra[i][a]);
        }

        int mid;
        while(l < r){
            mid = l + (r-l+1)/2;  //刘汝佳的二分写法,可以避免R=L+1与R=L都可能有解的情况
            if(test(mid)) l = mid;
            else r = mid-1;
        }
        printf("%d\n", l);
    }
    return 0;
}

贪心+二分

#include <bits/stdc++.h>
using namespace std;
struct plane{
    int time, pos, id;
}pla[4010];
int mark[2010];
bool operator < (const plane& a, const plane& b){
    return a.time < b.time;
}
int n, flag;
void add_plane(int pos, int time, int id){
    pla[pos].time = time, pla[pos].pos = pos % 2; //pos == 0 为Left,early
    pla[pos].id = id;
}

bool dfs(int s, int mid){
    mark[pla[s].id] = 1;
    int cnt = 1, pre = pla[s++].time;
    while(s < 2*n && cnt < n){
        if(pla[s].time-pre < mid && pla[s].pos && !mark[pla[s].id]) return false;
        if(pla[s].time-pre >=  mid){
            if(!mark[pla[s].id]) {
                cnt++;
                mark[pla[s].id] = 1;
                pre = pla[s].time;
                s++;
            }
            else s++;
        }
        else s++;
    }

    if(cnt!=n)
        return false;
    return true;
}

bool check(int mid){
    for(int i = 0; i <= flag; i++){
        memset(mark, 0, sizeof(mark));
        if(dfs(i, mid)) return true;
    }

    return false;
}

int main()
{
    //freopen("1.txt", "r", stdin);
    while(~scanf("%d", &n)){
        for(int i = 0; i < n; i++){
            int e, l;
            scanf("%d%d", &e, &l);
            add_plane(i*2, e, i);
            add_plane(i*2+1, l, i);
        }
        sort(pla, pla+2*n);
        for(flag = 0; flag < 2*n; flag++) if(pla[flag].pos) break;

        int l = 0, r = 1e7;
        int mid;
        while(r != l+1){
            mid = (r+l)/2;
            if(check(mid))
                l = mid;
            else
                r = mid-1;
        }
        if(check(l)) printf("%d\n", l);
        else printf("%d\n", r);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值