UVALive 3211 Now or later dichotomy + Twosat

Keywords: less

Topic link: https://vjudge.net/problem/UVALive-3211

Topic: Each aircraft has two ways of taking off and landing early and late. Given two ways of taking off and landing time of n aircraft, arrange the time of taking off and landing for each aircraft (early or late).

Late), so that all aircraft take-off and landing time in accordance with the order of morning to night between the minimum interval.

Solution: It is better to adopt the dichotomy method when the minimum time is as large as possible, and then it becomes to judge whether a certain time interval m meets the requirements.

Set up a variable xi, 0 for early and 1 for late without adding aircraft. Then each aircraft I is represented by two points 2*i,2*i+1, if the former is marked in a table.

The latter is marked as late descent. Then, constraints are established for i, j whose interval between take-off and landing time is less than m for different aircraft, such as I aircraft.

If the early landing interval and the early landing interval of J aircraft are less than m, the constraints can be expressed as follows: 2*i-> 2*j+1 on two sides is established.

2 * J - > 2 * i + 1, which means that if i falls early, J must fall late and j must fall early and i must fall late.

///UVALIVE 3211

#include <bits/stdc++.h>
using namespace std;
const int maxn = 4000010;
struct TwoSAT{
    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]=1;
        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 addedge(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;
    }
}twosat;

int n, T[maxn][2];
bool check(int mid){
    twosat.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])<mid){
                        twosat.addedge(i,a^1,j,b^1);
                    }
                }
            }
        }
    }
    return twosat.solve();
}

int main(){
    while(scanf("%d", &n)!=EOF&&n){
        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]);
            }
        }
        int ans = 0;
        while(l<=r){
            int mid=(l+r)/2;
            if(check(mid)) ans=mid,l=mid+1;
            else r=mid-1;
        }
        printf("%d\n", ans);
    }
    return 0;
}

Posted by Drizzt321 on Sun, 10 Feb 2019 01:18:16 -0800