POJ 2296 Map Labeler

题目链接

题意:

坐标轴上有N个点。要在每一个点上贴一个正方形,这个正方形的横竖边分别和x,y轴平行,而且要使得点要么在正方形的上面那条边的中点,或者在以下那条边的中点。而且随意两个点的正方形都不重叠(能够重边)。问正方形最大边长能够多少?

思路:显然的2-sat问题,注意推断两个矩形相交的地方,细节

代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std; const int MAXNODE = 205; struct TwoSet {
int n;
vector<int> g[MAXNODE * 2];
bool mark[MAXNODE * 2];
int S[MAXNODE * 2], sn; void init(int tot) {
n = tot * 2;
for (int i = 0; i < n; i += 2) {
g[i].clear();
g[i^1].clear();
}
memset(mark, false, sizeof(mark));
} void add_Edge(int u, int uval, int v, int vval) {
u = u * 2 + uval;
v = v * 2 + vval;
g[u^1].push_back(v);
g[v^1].push_back(u);
} void delete_Edge(int u, int uval, int v, int vval) {
u = u * 2 + uval;
v = v * 2 + vval;
g[u^1].pop_back();
g[v^1].pop_back();
} bool dfs(int u) {
if (mark[u^1]) return false;
if (mark[u]) return true;
mark[u] = true;
S[sn++] = u;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (!dfs(v)) return false;
}
return true;
} bool solve() {
for (int i = 0; i < n; i += 2) {
if (!mark[i] && !mark[i + 1]) {
sn = 0;
if (!dfs(i)){
for (int j = 0; j < sn; j++)
mark[S[j]] = false;
sn = 0;
if (!dfs(i + 1)) return false;
}
}
}
return true;
}
} gao; const int N = 105; int t, n;
struct Point {
int x, y;
void read() {
scanf("%d%d", &x, &y);
x *= 2;
}
} p[N]; bool judge(int len) {
gao.init(n);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (p[i].x + len <= p[j].x - len || p[j].x + len <= p[i].x - len) continue;
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
int y1, y2, y3, y4;
if (x == 0) {
y1 = p[i].y - len;
y2 = p[i].y;
} else {
y1 = p[i].y;
y2 = p[i].y + len;
}
if (y == 0) {
y3 = p[j].y - len;
y4 = p[j].y;
} else {
y3 = p[j].y;
y4 = p[j].y + len;
}
if ((y1 >= y3 && y1 < y4)
|| (y2 > y3 && y2 <= y4)
|| (y3 >= y1 && y3 < y2)
|| (y3 > y2 && y4 <= y2))
gao.add_Edge(i, x, j, y);
}
}
}
}
return gao.solve();
} int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++)
p[i].read();
int l = 0, r = 20000;
while (l < r) {
int mid = (l + r) / 2;
if (judge(mid)) l = mid + 1;
else r = mid;
}
printf("%d\n", l - 1);
}
return 0;
}

POJ 2296 Map Labeler(2-sat)的更多相关文章

  1. POJ 2296 Map Labeler (2-Sat)

    Map Labeler Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1267   Accepted: 409 Descri ...

  2. POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat 二分)

    POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat ...

  3. 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 ...

  4. POJ 2296 Map Labeler

    二分答案 + 2-SAT验证,判断正方形是否相交写起来有点烦,思路还是挺简单的. #include<cstdio> #include<cstring> #include< ...

  5. POJ 2251 Dungeon Master(地牢大师)

    p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...

  6. poj 3335 Rotating Scoreboard(半平面交)

    Rotating Scoreboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6420   Accepted: 25 ...

  7. C++ map使用(基于hashtable)

    C++ map使用(基于hashtable) 实际上基于hashtable的map有两种一种是hash_map,unordered_map,但是最好使用后者,原因如下[1] 因为标准化的推进,unor ...

  8. POJ 2376 Cleaning Shifts(轮班打扫)

    POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Farmer ...

  9. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

随机推荐

  1. HDU 4341

    分组背包而已.注意的是,每个时间T,要把一组的全加进去比较一次. #include <iostream> #include <cstdio> #include <cstr ...

  2. CF 372B Counting Rectangles is Fun [dp+数据维护]

    题意,给出一个n行m列的矩阵 里面元素是0或者1 给出q个询问 a,b,c,d 求(a,b)到(c,d)有多少个由0组成的矩形 我们定义 watermark/2/text/aHR0cDovL2Jsb2 ...

  3. 创建cifs系统案例之“实现将Windows磁盘共享至Linux”

    原创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong ...

  4. inconsistent line endings 解决方法

     I'm using Unity 3D in combination with Visual Studio 2008 on a Windows 7 64 bit system. When savi ...

  5. poj3685(嵌套二分)

    Matrix Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 4658   Accepted: 1189 Descriptio ...

  6. JavaScript——BOM(浏览器对象模型),时间间隔和暂停

    BOM(浏览器对象模型):能够对浏览器的窗体进行訪问和操作 1.主要的BOM体系: window------------document-------------------------------- ...

  7. angularjs1-3,工具方法,bootstrap,多个module,引入jquery

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. js 预加载图片image()函数

    创建一个Image对象:var a=new Image();    定义Image对象的src: a.src=”xxx.gif”;    这样做就相当于给浏览器缓存了一张图片. 图像对象: 建立图像对 ...

  9. C++四舍五入问题

    C++四舍五入问题: c++默认的流输出数值有效位是6,包括整数和小数,若数值超出6位,则第七位四舍五入到6位数 #include <iomanip> 输入输出库的一部分,声明了一些与提取 ...

  10. windows phone LongListSelector加载下一页

    LongListSelector利用ListHeader.ListFooter加载上一页和下一页XAML代码: <phone:LongListSelector> <phone:Lon ...