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;
}
UVa 3211 Now or later (二分+2-Sat)的更多相关文章
- UVa 714 Copying Books(二分)
题目链接: 传送门 Copying Books Time Limit: 3000MS Memory Limit: 32768 KB Description Before the inventi ...
- UVA 10816 + HDU 1839 Dijstra + 二分 (待研究)
UVA 题意:两个绿洲之间是沙漠,沙漠的温度不同,告诉起点,终点,求使得从起点到终点的最高温度最小的路径,如果有多条,输出长度最短的路径: 思路:用最小费用(最短路径)最大流(最小温度)也能搞吧,但因 ...
- UVA 10668 - Expanding Rods(数学+二分)
UVA 10668 - Expanding Rods 题目链接 题意:给定一个铁棒,如图中加热会变成一段圆弧,长度为L′=(1+nc)l,问这时和原来位置的高度之差 思路:画一下图能够非常easy推出 ...
- uva 10566 Crossed Ladders (二分)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 10539 (筛素数、二分查找) Almost Prime Numbers
题意: 求正整数L和U之间有多少个整数x满足形如x=pk 这种形式,其中p为素数,k>1 分析: 首先筛出1e6内的素数,枚举每个素数求出1e12内所有满足条件的数,然后排序. 对于L和U,二分 ...
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
- UVA 1335 Beijing Guards(二分答案)
入口: https://cn.vjudge.net/problem/UVA-1335 [题意] 有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个 ...
- UVa 12715 Watching the Kangaroo(二分)
题意:n条线段(n <= 100000) (L<=R <= 1e9) ,m组询问(m <= 100000) 每次询问一个点的覆盖范围的最大值.一个点x对于一条包括其的线段,覆盖 ...
- UVA 11865 Stream My Contest (二分+最小树形图)
题意:给定一个网络,一个服务器,其他的是客户机,有 m 条连线,每条有一个带宽和花费(单向边),让你用不超过 c 的花费,使得 0 到 所有的机器都能到达,并且使得最小带宽最大. 析:很明显是二分题, ...
随机推荐
- 卷积神经网络之VGG网络模型学习
VGG:VERY DEEP CONVOLUTIONAL NETWORKS FOR LARGE-SCALE IMAGE RECOGNITION 牛津大学 visual geometry group(VG ...
- StringsUtil字符串工具类---灵活截取
package com.js.ai.modules.pointwall.interfac; import javax.print.attribute.standard.MediaName; publi ...
- CodeSmith介绍和常见问题解决方案
一.CodeSmith介绍 CodeSmith模板代码生成实战详解 https://www.cnblogs.com/knowledgesea/p/5016077.html 二.CodeSmith连接 ...
- mfc对话框启动就直接隐藏在右下角显示托盘图标
之前在网络上找了一份mfc对话框启动就直接隐藏窗体,在右下角显示托盘图标的demo 在win7下可以正常使用,但在win10上直接溢出,导致计算机跟注销似的崩溃现象. 后来找到原因是因为在OnInit ...
- innobackupex 备份数据搭建 MySQL Slave
简介: 数据量比较大时,使用 innobackupex 备份数据新增 MySQL Slave 节点. 安装 innobackupex 工具,我这里写过一次:http://www.cnblogs.com ...
- 学习Java必看书籍和步骤(转载)
原地址:http://blog.csdn.net/yongjian1092/article/details/7372678 Java语言基础 谈到Java语言基础学习的书籍,大家肯定会推荐Bruce ...
- Dreamweaver安装与破解
1.下载Dreamweaver cs6破解版解压包 打开下载网址http://pan.baidu.com/s/1jGr8pbK,点击下载,保存到自己想要保存的位置. 2.下载Dreamweaver c ...
- 从socket开始讲IOS网络编程
home list tags talk user rss Mac&iOS Socket 大纲 一.Socket简介 二.BSD Socket编程准备 1.地址 2.端口 3.网络字节序 4.半 ...
- 使apk具有system权限
使apk具有system权限的方法: 方法一: 1. 在应用程序的AndroidManifest.xml中的manifest节点中加入 android:sharedUserId=" ...
- (转)libvirt API的基本概念
本文摘自:http://blog.sina.com.cn/s/blog_da4487c40102v31i.html libvirt对象 libvirt的对象向外展现了虚拟化环境的所有资源.libvir ...