UVALive3211- Now or later(二分+2-SAT)
题意:有n架飞机。每架飞机都能够选择早着陆和晚着陆两种方式之中的一个,且必须选择一种。
任务就是安排全部飞机着陆时。相邻两个着陆时间间隔的最小值尽量大。
思路:用二分处理最小值尽量大。该题目能够转化为是否存在一个调度方案,使得相邻两个着陆时间差总是不小于P,进一步转化为随意两个着陆时间差总是不小于P。
,如果布尔变量xi表示第i架飞机是否早着陆。唯一限制就是“时间差小于P的两个着陆时间不能同一时候满足。每一组不能同一时候满足的着陆时间相应一个子句,则整个约束条件相应于2-SAT问题的实例。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAXN = 2005; 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] = 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));
} 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 solver;
int n, T[MAXN][2]; bool test(int diff) {
solver.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]) < diff)
solver.add_clause(i, a^1, j, b^1);
return solver.solve();
} int main() {
while (scanf("%d", &n) != EOF) {
memset(T, 0, sizeof(T));
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]);
}
while (L < R) {
int mid = L + (R - L + 1) / 2;
if (test(mid))
L = mid;
else
R = mid - 1;
}
printf("%d\n", L);
}
return 0;
}
UVALive3211- Now or later(二分+2-SAT)的更多相关文章
- UVALive-3211 Now or later (2-SAT+二分)
题目大意:有n架飞机,每架飞机有两个可选择的着陆时间,并且每架飞机都必须要选一个时间着陆.为了安全考虑,要求两架飞机的最小着陆时间差最大,找出这个最大值. 题目分析:有“最小值的最大值”这样的字眼,用 ...
- LA 3211 飞机调度(2—SAT)
https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...
- 证明与计算(3): 二分决策图(Binary Decision Diagram, BDD)
0x01 布尔代数(Boolean algebra) 大名鼎鼎鼎的stephen wolfram在2015年的时候写了一篇介绍George Boole的文章:George Boole: A 200-Y ...
- Map Labeler POJ - 2296(2 - sat 具体关系建边)
题意: 给出n个点 让求这n个点所能建成的正方形的最大边长,要求不覆盖,且这n个点在正方形上或下边的中点位置 解析: 当然是二分,但建图就有点还行..比较难想..行吧...我太垃圾... 2 - s ...
- UVALive - 3211 (2-SAT + 二分)
layout: post title: 训练指南 UVALive - 3211 (2-SAT + 二分) author: "luowentaoaa" catalog: true m ...
- hdu3715 2-sat+二分
Go Deeper 题意:确定一个0/1数组(size:n)使得满足最多的条件数.条件在数组a,b,c给出. 吐槽:哎,一水提,还搞了很久!关键是抽象出题目模型(如上的一句话).以后做二sat:有哪些 ...
- POJ 2749 2SAT判定+二分
题意:图上n个点,使每个点都与俩个中转点的其中一个相连(二选一,典型2-sat),并使任意两点最大 距离最小(最大最小,2分答案),有些点相互hata,不能选同一个中转点,有些点相互LOVE,必需选相 ...
- 2 - sat 模板(自用)
2-sat一个变量两种状态符合条件的状态建边找强连通,两两成立1 - n 为第一状态(n + 1) - (n + n) 为第二状态 例题模板 链接一 POJ 3207 Ikki's Story IV ...
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8748 Solved: 3835[Submi ...
- BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]
2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 3352 Solved: 919[Submit][Stat ...
随机推荐
- react中的jsx详细理解
这是官网上的一个简单的例子 const name = 'Josh Perez'; const element = <h1>Hello, {name}</h1>; ReactDO ...
- C++中vector用法
在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. 1 基本操作 (1)头文件#include<vector>. (2)创建vector对象,vector<in ...
- Java任务执行计时
Long startTime = System.currentTimeMillis(); Long endTime = System.currentTimeMillis(); endTime-star ...
- 天梯赛L1 题解
L1-001 Hello World (5 分) 这道超级简单的题目没有任何输入. 你只需要在一行中输出著名短句“Hello World!”就可以了. AC代码:(直接输出记性) #include & ...
- django实现github第三方本地登录
1.安装 pip install social-auth-app-django 2.生成Client ID和Client Secret 3.修改setting.py INSTALLED_APPS = ...
- JS模块之AMD, CMD, CommonJS、UMD和ES6模块
CommonJS 传送门 同步加载,适合服务器开发,node实现了commonJS.module.exports和require 判断commonJS环境的方式是(参考jquery源码): if ( ...
- python-字典数据类型内置方法
字典数据类型内置方法(必考) 用途:存多个值,不通过索引取值,可以用关键字找到对应得值 定义方式:{}内以key:value的方式存储多个值,值与值之间用逗号隔开 lis = ['ruixing', ...
- 实验楼Python学习记录_挑战字符串操作
自我学习记录 Python3 挑战实验 -- 字符串操作 目标 在/home/shiyanlou/Code创建一个 名为 FindDigits.py 的Python 脚本,请读取一串字符串并且把其中所 ...
- php 复制文件夹
public function recurse_copy($src,$des) { $dir = opendir($src); @mkdir($des); while(false !== ( $fil ...
- java中String数组和List的互相转化
1.List转String数组 方法一: //先准备一个List List<String> testList=new ArrayList<>(); testList.add(& ...