# Uvalive -- 3211 -- now or later [2-sat + binary answer]

Source: Internet
Author: User

Link:Https://icpcarchive.ecs.baylor.edu/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 1212

Question:There are n planes that need to land. Each plane can choose either "early landing" or "late landing". The early landing time of the I plane is EI and the late landing time is Li, do not land at other times. Your task is to arrange landing methods for these planes so that the entire landing plan is as safe as possible. In other words, if the actual landing time of all aircraft is sorted from small to large, the minimum value of the two adjacent landing time intervals should be as large as possible.

Ideas:This is the 2-Sat example in Liu rujia's white book. The template in the book is used, and the idea is also based on his.

The typical solution for "minimum value as big as possible" is to find the final answer P in binary search. In this way, the original problem is converted to the question "whether there is a scheduling scheme, so that the time difference between the two adjacent lands is always no less than P", and this problem can be further converted: the time difference between any two lands is always no less than P. If the bool variable Xi indicates whether the I-th plane has landed early, the only restriction is that "the two landing times with a time difference less than P cannot meet the requirements at the same time ". Each set of landing times that cannot be met at the same time corresponds to a clause, then the entire constraint corresponds to an instance with a 2-Sat question, including n variables and N * (n-1) /Two clauses.

`#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 2010#define eps 1e-7#define INF 0x3F3F3F3F      //0x7FFFFFFF#define LLINF 0x7FFFFFFFFFFFFFFF#define seed 1313131#define MOD 1000000007#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1struct 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] = 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));    }    //x = xval or y = yval    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;    }};TwoSAT fuck;int n, T[MAXN];bool gao(int diff){    int i, j;    fuck.init(n);    for(i = 0; i < n; i++){        for(int a = 0; a < 2; a++){            for(j = i + 1; j < n; j++){                for(int b = 0; b < 2; b++){                    if(abs(T[i][a] - T[j][b]) < diff)                        fuck.add_clause(i, a ^ 1, j, b ^ 1);                }            }        }    }    return fuck.solve();}int main(){    int i, j;    while(scanf("%d", &n) != EOF){        int l = 0, r = 0;        for(i = 0; i < n; i++){            scanf("%d%d", &T[i], &T[i]);            int temp = max(T[i], T[i]);            r = max(r, temp);        }        int ans;        while(l <= r){            int mid = (l + r) / 2;            if(gao(mid)){                if(!gao(mid + 1)){                    ans = mid;                    break;                }                else    l = mid + 1;            }            else    r = mid - 1;        }        printf("%d\n", ans);    }    return 0;}`

Uvalive -- 3211 -- now or later [2-sat + binary answer]

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.