UVALive - 3211 - Now or later(图论——2-SAT)
Time Limit: 9000 mSec
Problem Description

Input

Output

Sample Input
Sample Output
10
题解:2-SAT问题板子题,这个问题主要是理论难度比较大,有了结论之后代码很容易,有专门的论文阐释算法的正确性,看了几位大佬写的,基本上明白是怎么一回事,理解不深刻,就不在这里胡扯了,直接上代码。
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0); struct TwoSAT
{
int n;
vector<int> G[maxn * ];
bool mark[maxn * ];
int S[maxn * ], c; bool dfs(int x)
{
if (mark[x ^ ])
return false;
if (mark[x])
return true;
mark[x] = true;
S[c++] = x;
for (auto v : G[x])
{
if (!dfs(v))
return false;
}
return true;
} void init(int n)
{
this->n = n;
for (int i = ; i < n * ; i++)
{
G[i].clear();
}
memset(mark, , sizeof(mark));
} void add_clause(int x, int xval, int y, int yval)
{
x = x * + xval;
y = y * + yval;
G[x ^ ].push_back(y);
G[y ^ ].push_back(x);
} bool solve()
{
for (int i = ; i < n * ; i += )
{
if (!mark[i] && !mark[i + ])
{
c = ;
if (!dfs(i))
{
while (c > )
{
mark[S[--c]] = false;
}
if (!dfs(i + ))
return false;
}
}
}
return true;
}
}; TwoSAT solver; int n, T[maxn][]; bool Judge(int lim)
{
solver.init(n);
for (int i = ; i < n; i++)
{
for (int a = ; a < ; a++)
{
for (int j = i + ; j < n; j++)
{
for (int b = ; b < ; b++)
{
if (abs(T[i][a] - T[j][b]) < lim)
{
solver.add_clause(i, a ^ , j, b ^ );
}
}
}
}
}
return solver.solve();
} main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
while (cin >> n && n)
{
int le = , ri = ;
for (int i = ; i < n; i++)
{
for (int j = ; j < ; j++)
{
cin >> T[i][j];
ri = max(ri, T[i][j]);
}
} int ans = ;
while (le <= ri)
{
int mid = (le + ri) >> ;
if (Judge(mid))
{
ans = mid;
le = mid + ;
}
else
{
ri = mid - ;
}
}
cout << ans << endl;
}
return ;
}
UVALive - 3211 - Now or later(图论——2-SAT)的更多相关文章
- UVALive - 3211 (2-SAT + 二分)
layout: post title: 训练指南 UVALive - 3211 (2-SAT + 二分) author: "luowentaoaa" catalog: true m ...
- UVALive 3211 Now or later
每架飞机有早晚起降两种方式,给定n架飞机两种方式的起落时间,为每架飞机安排起落时间(早或晚),使得所有飞机起降时间按照早到晚的顺序之间的间隔时间最小值尽量大. 分析: 最小时间尽量大应该采用二分的方法 ...
- 2-sat基础题 uvalive 3211
蓝书325页的基础题 二分+2-sat //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using n ...
- UVALive 3211 Now or later(2-sat)
2-sat问题,一种在两种可能性中选择必然关系的问题. 推荐两篇论文,也是学2-sat公认比较好的材料.前者较好理解,后者需耐心看. http://www.google.com.hk/url?sa=t ...
- UVALive 3211 Now or later(2-SAT,二分,Kosaraju)
题意: 有n个飞机要降落,每机都可以在两个时间点上选择降落.但是两机的降落时间间隔太小会影响安全性,所以,要求两机的降落时间应该达到最大,当然也不能冲突了.问最大的时间间隔是多少?(其实问的是max( ...
- 【UVALive - 3211】Now or later (二分+2-SAT)
题意: 有n架飞机需要着陆.每架飞机有两种选择,早着陆或者晚着陆,二选其一.现在为了保证飞机的着陆安全,要求两架着陆的飞机的时间间隔的最小值达到最大. 分析: 最小值最大问题我们想到二分答案.对于猜测 ...
- 【二分答案+2-SAT】Now or later UVALive - 3211
题目链接:https://cn.vjudge.net/contest/209473#problem/J 题目大意: 有n架飞机,每架飞机有两个可降落时间点a,b(a<b)(即一架飞机可以选择在时 ...
- Now or later UVALive - 3211(2-SAT 最小值最大化)
emmm...去吃早饭了... rujia讲的很好.. 最小值最大化问题,,,二分枚举答案 设x1.x2为同一个集合中的元素,y1.y2为另一个集合中的元素,如果x1与y1之差小于mid,那么如果 ...
- UVALive - 3211 Now or later (二分+2SAT)
题目链接 题意:有n架飞机,每架飞机有两个着陆时间点可以选,要求任意两架飞机的着陆时间之差不超过k,求k的最大值. 解法:由于每架飞机都有两个选择,并且必选且只能选其中一个,时间冲突也是发生在两架飞机 ...
随机推荐
- Net Configuration Agent
提出Configuration Agent这样一个东西可能会让人感到奇怪,对于配置信息读取什么还需要一个Agent;那Agent的作用是什么,能达到一个怎样的目的,下面讲解为何需要Agent和其重要性 ...
- leetcode — candy
/** * Source : https://oj.leetcode.com/problems/candy/ * * There are N children standing in a line. ...
- leetcode — convert-sorted-list-to-binary-search-tree
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
- centos7修改网卡名字为传统名字
前言:在centos6及其之前的系统中,我们已经习惯了网卡为eth0,eht1这种网卡命名方式,在centos7上网卡名字为ens33,ens37,ens38等名字,很别扭,想统一下网卡的命名. 1 ...
- 如何解决svn清理失败 不能更新 cleanup失败 cleanup乱码 更新乱码 svn更新提示清理 清理乱码不能清理 svn故障修复SVN cleanup 陷入死循环 svn cleanup时遇到错误怎么办
平时使用svn的过程中,有的时候由于自己操作故障或者系统原因,导致svn不能更新,提示cleanup也不能成功,陷入了死循环 原因是;svn的数据库队列原因 1,下载sqlite3.exe,googl ...
- 浅析MySQL 8忘记密码处理方式
对MySQL有研究的读者,可能会发现MySQL更新很快,在安装方式上,MySQL提供了两种经典安装方式:解压式和一键式,虽然是两种安装方式,但我更提倡选择解压式安装,不仅快,还干净.在操作系统上,My ...
- java web 项目打包(war 包)并部署
1.在eclipse中右键单击项目,然后Export选择WAR file,生成项目的WAR文件.具体步骤请看图片详细操作步骤: 2.把生成的WAR文件放到tomcat解压之后的webapps文件夹下. ...
- 实时显示数据 SignalR 及时消息提醒( 立即向其推送内容)
实时显示数据 SignalR 及时消息提醒( 立即向其推送内容) http://www.cnblogs.com/Leo_wl/p/5634910.html <!--Reference the ...
- vue axios 批量删除 数组参数
方法一:前端循环请求服务器端delete(id)方法 请问如何获得element-ui表格中的勾选项index,以实现批量删除功能 https://segmentfault.com/q/1010000 ...
- 【Spring】6、注解大全
一.@interface Java用 @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类. 二.@Override,@Deprecated,@S ...