UVALive 3211 Now or Later (2-SAT)
题目的要求一个最小值最大,二分即可,但是怎么判断呢?
飞机早或者晚两种状态,可以用一个布尔变量表示,假设当前猜测为m,那么根据题意,
如果x和y所对应的时间冲突那么就是¬(xΛy)化成或的形式(¬x)V(¬y),就可以套用twoSAT了。
关于2-SAT,简单理解是,把逻辑推导变成一条有向边,然后跑图判断一下。
#include<bits/stdc++.h>
using namespace std; const int maxn = ; #define PB push_back
struct TwoSAT
{
int n,M;
vector<int> G[maxn*];
bool vis[maxn*];
int S[maxn*], c; void init(int n)
{
this->n = n;
M = n<<;
for(int i = ; i < M; i++) G[i].clear();
memset(vis,,sizeof(vis));
} bool dfs(int x)
{
if(vis[x^]) return false;
if(vis[x]) return true;
vis[x] = true;
S[c++] = x;
for(int i = ; i < (int)G[x].size(); i++){
if(!dfs(G[x][i])) return false;
}
return true; } void add_clause(int x,int xv,int y,int yv)// xv or yv
{
x = x<<|xv;
y = y<<|yv;
G[x^].PB(y);
G[y^].PB(x);
} bool solve()
{
for(int i = ; i < M; i+=){
if(!vis[i] && !vis[i+]){
c = ;
if(!dfs(i)){
while(c>) vis[S[--c]] = false;
if(!dfs(i+)) return false;
}
}
}
return true;
}
}solver; int T[maxn][]; bool ok(int m,int n)
{
solver.init(n);
for(int i = ; i < n; i++){
for(int xv = ; xv < ; xv++){
for(int j = i+; j < n; j++){
for(int yv = ; yv < ; yv++){
if(abs(T[i][xv]-T[j][yv])<m){
solver.add_clause(i,xv^,j,yv^);
}
}
}
}
}
return solver.solve();
} int main()
{
//freopen("in.txt","r",stdin);
int n;
while(~scanf("%d",&n)){
int l = , r = ;
for(int i = ; i < n; i++){
scanf("%d%d",T[i],T[i]+);
r = max(r,max(T[i][],T[i][]));
}
int mid;
for( ; l < r; ok(mid,n)?l = mid:r = mid-) mid = (l+r+)>>;
printf("%d\n",l);
}
return ;
}
UVALive 3211 Now or Later (2-SAT)的更多相关文章
- UVALive - 3211 - Now or later(图论——2-SAT)
Problem UVALive - 3211 - Now or later Time Limit: 9000 mSec Problem Description Input Output Sampl ...
- UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...
- LA 3211 飞机调度(2—SAT)
https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...
- UVALive 3211 Now or later(2-sat)
2-sat问题,一种在两种可能性中选择必然关系的问题. 推荐两篇论文,也是学2-sat公认比较好的材料.前者较好理解,后者需耐心看. http://www.google.com.hk/url?sa=t ...
- POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang
Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...
- 学习笔记(two sat)
关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...
- UVALive 6609 Minimal Subarray Length(RMQ-ST+二分)
题意:给定长度为N的数组,求一段连续的元素之和大于等于K,并且让这段元素的长度最小,输出最小长度即可,若不存在这样的元素集合,则输出-1 题目链接:UVAlive 6609 做法:做一个前缀和pref ...
- UVALive 5903 Piece it together(二分图匹配)
给你一个n*m的矩阵,每个点为'B'或'W'或'.'.然后你有一种碎片.碎片可以旋转,问可否用这种碎片精确覆盖矩阵.N,M<=500 WB <==碎片 W 题目一看,感觉是精确覆盖(最近 ...
- UVALive 2403 77377解题报告(深搜)
题意:给你一些固定的字符串,在给出数字,根据键盘的对应关系,输出所有的满足条件的字符串,输出顺序无所谓. 思路:因为题目说了,输出比较小,说明测试数据并不强,所以可以暴力回溯求出答案,将所有的给出的字 ...
随机推荐
- outlook2013 解决附件大小限制
1.先关闭outlook,然后点击"运行"-->输入"regedit" #打开注册表 2.依次打开 “HKEY_CURRENT_USER\Softwar ...
- 4-3逻辑非运算符及案例 & 4-4
创建类 LoginDemo3 这里取反 !(n%3==0) package com.imooc.operator; import java.util.Scanner; public class Log ...
- spown mj
local function getmjvalnew(key) local keynew = {} local sumnval = 0 for _, v in ipairs(key) do if v& ...
- Git 移除某些文件
一.前言 在使用 Git 版本控制中,有些文件是不需要加入到版本控制中的.如 日志( log ).编译的文件.这些随时都在变的文件,使用用一个代码库的用户.只要稍稍修改一点,或者启动一下,就会变.容易 ...
- 201621123016《Java程序设计》第二周学习总结
1. 本周学习总结 1.本周学习重点: 字符串常量池:这是java中为减少字符串的建立而设计的,在没有使用new构建字符串时,jvm会从字符串常量池查找其值,如果没有则会创建这个字符串再把其放在字符串 ...
- LCT 动态树 模板
洛谷:P3690 [模板]Link Cut Tree (动态树) /*诸多细节,不注意就会调死去! 见注释.*/ #include<cstdio> #include<iostream ...
- Eclipse开发MR环境搭建
1.jdk环境配置 jdk安装后好后配置相关JAVA_HOME环境变量,并将bin目录配置到path 2. 下载hadoop-2.7.1.tar.gz 解压hadoop-2.7.1.tar.g ...
- beanutils包下载
- 自定义收索View
1 .h文件 @interface SearchNavView : UIView @property (nonatomic, copy) void(^cancleBtnBlock)(void); @p ...
- python模块之hmac
# hmac模块使用步骤: # hmac模块模块的使用步骤与hashlib模块的使用步骤基本一致,只是在第1步获取hmac对象时,只能使用hmac.new()函数, # 因为hmac模块没有提供与具体 ...