UVA1146 / LA3211(ACM-ICPC 2004 Europe - Southwestern) Now or later(2-SAT问题 + 二分答案)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

题目要求为 最大化最小值,很明显就是二分答案。
题目中每个飞机 要么是一种状态(早),要么是另一种状态(晚),考虑 2-SAT。

我们二分答案,二分着陆时间间隔的最小值 x。

枚举每两个飞机 p , q 的 4 种状态

设现在枚举的这个情况中,p 的着陆时间为 a,q 的着陆时间为 b,则若 ∣ a − b ∣ < x |a-b|<x ab<x那么就要在相应的状态间连边(表示这两个状态只能二选一,也就是询问所有间隔小于x 的边所相连的两个状态都不能同时成立能否被满足)。

注意本题中数据有重边,要用vector,不然会RE

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>

using namespace std;
typedef long long ll;
const int N = 5000007, M = 5000007, INF = 0x3f3f3f3f;

int n, m;
int dfn[N], low[N], num;
int head[N], ver[M], nex[M], tot;
vector<int>g[N];
int a[N][2];
int stk[N], top, scc_cnt;
bool ins[N];
int color[N];

void add(int x, int y){
    
    
    ver[tot] = y;
    nex[tot] = head[x];
    head[x] = tot ++ ;
}

void tarjan(int x)
{
    
    
    dfn[x] = low[x] = ++ num;
    stk[++ top] = x;
    ins[x] = true;
    for(size_t i = 0; i < g[x].size(); ++ i){
    
    
        int y = g[x][i];
        if(!dfn[y]){
    
    
            tarjan(y);
            low[x] = min(low[x], low[y]);
        }
        else if(ins[y])
            low[x] = min(low[x], dfn[y]);
    }
    if(low[x] == dfn[x]){
    
    
        int y;
        ++ scc_cnt;
        color[x] = scc_cnt;
        do{
    
    
            y = stk[top -- ];
            ins[y] = false;
            color[y] = scc_cnt;
        }while(x != y);
    }
    return ;
}

inline bool check(int x){
    
    
    //memset(head, -1, sizeof head);
    for(int i = 1; i <= 2 * n; ++ i)
        g[i].clear();
    memset(dfn, 0, sizeof dfn);
    memset(low, 0, sizeof low);
    memset(color, 0, sizeof color);
    memset(ins, 0, sizeof ins);
    tot = num = scc_cnt = 0;

    //0 : -x : E 早 : i
    //1 : x : L 晚 : i + n

    //比较之后的两个相差的时间是否小于x
    //如果小于x的话那么两者就不能同时出现(同时为true)
    for(int p = 1;p <= n;++ p){
    
    
        for(int c = 0; c <= 1; ++ c){
    
    
            for(int q = p + 1; q <= n; ++ q){
    
    
                for(int d = 0; d <= 1; ++ d){
    
    
                    if(abs(a[p][c] - a[q][d]) < x){
    
    
                        /*if(c && d){//11 -> 01, 01
                            add(p, q + n);
                            add(q, p + n);
                        }
                        else if(!c && d){//01 -> 11, 00
                            add(p + n, q + n);
                            add(q, p);
                        }
                        else if(c && !d){//10 -> 00, 11
                            add(p, q);
                            add(q + n, p + n);
                        }
                        else if(!c && !d){//00 -> 10, 01
                            add(p + n, q);
                            add(q + n, p);
                        }*/
                        g[p + n * c].push_back(q + n * (d ^ 1));
                        g[q + n * d].push_back(p + n * (c ^ 1));
                    }
                }
            }
        }
    }

    for(int i = 1; i <= 2 * n; ++ i)
        if(!dfn[i])
            tarjan(i);

    for(int i = 1; i <= n; ++ i){
    
    
        if(color[i] == color[i + n])
            return false;
    }
    return true;
}

void solve(){
    
    
    int l = 0, r = 0, ans = -1;

    for(int i =1; i <= n; ++ i)
            scanf("%d%d", &a[i][0], &a[i][1]), r = max(max(a[i][1], a[i][0]), r);


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

int main()
{
    
    
    while(scanf("%d", &n) != EOF){
    
    
        solve();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45697774/article/details/108715450