UVa 3211 Now or later (二分+2-Sat)

时间:2023-03-09 20:15:13
UVa 3211 Now or later (二分+2-Sat)

题意:有 n 架飞机,每个飞机早着陆,或者晚着陆,让你安排一个方式,让他们着陆的时间间隔尽量大。

析:首先对于时间间隔,可以用二分来解决,然后就成了一个判定性问题,然后怎么判断该时间间隔是不是成立呢,那么用2-Sat能解决,每次对于时间间隔都小于正在判定的,然后给他们连上相应的边,是连两条,然后跑一遍2-sat 就OK了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e15;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2000 + 10;
const int mod = 3;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} // UVaLive 3211 int ta[maxn], tb[maxn]; struct TwoSAT{
int n;
vector<int> G[maxn<<1];
bool mark[maxn<<1];
int S[maxn<<1], 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].sz; ++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].cl;
ms(mark, 0);
} void add_clause(int x, int xval, int y, int yval){
x = x * 2 + xval;
y = y * 2 + yval;
G[x^1].pb(y);
G[y^1].pb(x);
} bool solve(){
for(int i = 0; i < 2*n; i += 2){
if(!mark[i] && !mark[i+1]){
c = 0;
if(!dfs(i)){
while(c > 0) mark[S[--c]] = 0;
if(!dfs(i+1)) return false;
}
}
}
return true;
}
}; TwoSAT twosat; bool judge(int m){
twosat.init(n);
for(int i = 0; i < n; ++i)
for(int j = i+1; j < n; ++j){
if(abs(ta[i] - ta[j]) <= m) twosat.add_clause(i, 0, j, 0);
if(abs(ta[i] - tb[j]) <= m) twosat.add_clause(i, 0, j, 1);
if(abs(tb[i] - ta[j]) <= m) twosat.add_clause(i, 1, j, 0);
if(abs(tb[i] - tb[j]) <= m) twosat.add_clause(i, 1, j, 1);
}
return twosat.solve();
} int main(){
while(scanf("%d", &n) == 1){
int l = 0, r = 0;
for(int i = 0; i < n; ++i){
scanf("%d %d", ta+i, tb+i);
r = max(r, tb[i]);
}
while(l <= r){
int m = l + r >> 1;
if(judge(m)) l = m + 1;
else r = m - 1;
}
printf("%d\n", l);
}
return 0;
}