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 二分)
Description
Map generation is a difficult task in cartography. A vital part of such task is automatic labeling of the cities in a map; where for each city there is text label to be attached to its location, so that no two labels overlap. In this problem, we are concerned with a simple case of automatic map labeling.
Assume that each city is a point on the plane, and its label is a text bounded in a square with edges parallel to x and y axis. The label of each city should be located such that the city point appears exactly in the middle of the top or bottom edges of the label. In a good labeling, the square labels are all of the same size, and no two labels overlap, although they may share one edge. Figure 1 depicts an example of a good labeling (the texts of the labels are not shown.)
Given the coordinates of all city points on the map as integer values, you are to find the maximum label size (an integer value) such that a good labeling exists for the map.
Input
The first line contains a single integer t (1 <= t <= 10), the number of test cases. Each test case starts with a line containing an integer m (3 ≤ m ≤ 100), the number of cities followed by m lines of data each containing a pair of integers; the first integer (X) is the x and the second one (Y) is the y coordinates of one city on the map (-10000 ≤X, Y≤ 10000). Note that no two cities have the same (x, y) coordinates.
Output
The output will be one line per each test case containing the maximum possible label size (an integer value) for a good labeling.
Sample Input
1
6
1 1
2 3
3 2
4 4
10 4
2 5
Sample Output
2
Http
POJ:https://vjudge.net/problem/POJ-2296
ZOJ:https://vjudge.net/problem/ZOJ-2493
HIT:https://vjudge.net/problem/HIT-2369
UVAlive:https://vjudge.net/problem/UVALive-2973
Source
2-sat 二分
题目大意
在平面直角坐标系上有n个点,现在要在这些点之间填充正方形,并且满足每个点都在正方形的上边中点或下边中点。正方形不能重叠,但可以共用一条边。现在要求出满足条件的最大边长正方形的边长。
解决思路
看到这种最小值最大或是最大值最小的题目,立刻能想到二分。所以我们二分边长,然后判断这个边长是否满足题意。
那么现在的关键就是:如何判断某个边长是否,满足题意?
通过读题,我们发现,一个点填充正方形无疑只有两种情况,这个点在正方形的下边中点和在正方形的上边中点。一个点有两种状态,所以我们可以用2-sat来判断可行性。那么我们定义i表示正方形朝上,i+n表示正方形朝下。为了方便起见,在下文中,我们用Diff代表当前要判断的正方形边长。
那么如何建图呢?
我们知道,2-sat中一条有向边i->j表示选i必须选j,那么在这个题中,我们就是要找出两个正方形若一个怎么样另一个必须怎么样。
首先根据简单的推理可得,若两正方形(一下皆用i,j表示,为了方便叙述,我们假定i的纵坐标>=j的纵坐标)横坐标之差>=Diff,或是纵坐标之差>=2*Diff则两个正方形无论怎么摆都不会互相干扰,可以直接跳过。
然后我们分情况讨论:
1.纵坐标相等的时候(yi==yj)
我们发现这时i与j必然要一个向上一个向下。所以我们连接边i->j+n(若i向上则j必须向下),连接边i+n->j(若i向下则j向上),j->i+n,j+n->i(同理)
2.纵坐标之差大于0但小于Diff
这时两个点之间无法摆下一个正方形,所以上面的点必须向上摆,下面的点必须向下摆。
但这样如何连边呢?我们连上i+n->i(i若向下则i必须向上),j->j+n(j若向上则j必须向下)。这样是不是很奇怪?为什么么会有这样的边出现?没错,这就是刻意制造一种矛盾,使得选i+n时必选i,且在后面判断可行性的时候若i+n与i在同一强连通分量则不符合题意,无解。这里就是用来保证i必须向上,而j必须向下。另外,为了保险起见,还可以连边i->j+n和j+n->i。
3.纵坐标之差大于等于Diff但小于2*Diff
此时,两个点之间是可以摆下一个正方形的,所以有:若上面的向下,则下面的必须向下;若下面的向上,则上面的必须向上(因为最多只能摆下一个正方形)。表述成边就是:i+n->j+n,j->i。
注意此时并不需要连接i->j或是j+n->i+n,因为当上面的向上时,下面的既可以向上也可以向下;同理,下面的向下时,上面的既可以向上也可以向下。
建完图后就是判断可行性了。这里推荐的方法是用Tarjan缩点求强联通分量再看i和i+n是否在同一强连通分量中(若是则说明无解,否则有解)。鉴于前面的博客已经详细讲过如何实现,请到这里查看。
另一种方法是用暴力染色法,前文也已经叙述过了,可以在这里查看。
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<stack>
#include<cmath>
using namespace std;
#define mem(Arr,x) memset(Arr,x,sizeof(Arr))
const int maxN=500;
const int inf=2147483647;
int n;
int Readx[maxN];//读入
int Ready[maxN];
vector<int> E[maxN];//存下2-sat图
//tajan //Tarjan算法要用到的变量,不多说
int cnt;
int dfn[maxN];
int low[maxN];
bool instack[maxN];
stack<int> S;
//LTK //强连通分量
int LTKcnt;
int Belong[maxN];//原图中的每个点所属的强连通分量编号
void init();
void Graph_init(int Diff);
void Link(int u,int v);
bool check();
void tarjan(int u);
int main()
{
int T;
cin>>T;
for (int ti=1;ti<=T;ti++)
{
cin>>n;
init();
for (int i=1;i<=n;i++)
scanf("%d%d",&Readx[i],&Ready[i]);
int l=0,r=20000;
int Ans;
do//二分答案
{
init();
int mid=(l+r)/2;
Graph_init(mid);//每次检查前都要重新初始化
if (check())//检查Mid是否满足
{
Ans=mid;
l=mid+1;
}
else
r=mid;
}
while (l<r);
cout<<Ans<<endl;
}
return 0;
}
void init()
{
cnt=LTKcnt=0;
mem(dfn,0);
mem(instack,0);
for (int i=0;i<=n*2;i++)
E[i].clear();
while (!S.empty())
S.pop();
return;
}
void Graph_init(int Diff)
{
for (int i=1;i<=n;i++)
for (int j=i+1;j<=n;j++)
if (abs(Readx[i]-Readx[j])<Diff)//i表示往上,i+n表示往下
{
int maxy,miny;
if (Ready[i]>Ready[j])
{
maxy=i;
miny=j;
}
else
{
maxy=j;
miny=i;
}
if (Ready[maxy]==Ready[miny])//纵坐标相同时,必有一上一下
{
Link(miny,maxy+n);//如果下面的往上,上面的的必须往下
Link(maxy,miny+n);//如果上面的往上,下面的的必须往下
Link(miny+n,maxy);//同理
Link(maxy+n,miny);
continue;
}
if ((Ready[maxy]-Ready[miny]<2*Diff)&&(Ready[maxy]-Ready[miny]>=Diff))
{
Link(miny,maxy);//如果下面的往上,上面的必须往上
Link(maxy+n,miny+n);//如果上面的往下,下面的必须往下
continue;
}
if ((Ready[maxy]-Ready[miny]<Diff))
{
Link(miny,miny+n);//下面的必须往下
Link(maxy+n,maxy);//上面的必须往上
Link(miny+n,maxy);//下面的必须往下,上面的必须往上
Link(maxy,miny+n);//上面的必须往上,下面的必须往下
}
}
return;
}
void Link(int u,int v)
{
E[u].push_back(v);
return;
}
bool check()
{
for (int i=1;i<=2*n;i++)
if (dfn[i]==0)
{
tarjan(i);
}
for (int i=1;i<=n;i++)
if (Belong[i]==Belong[i+n])//如果i与i+n在同一强连通分量则说明矛盾(一个正方形不可能又往上又往下)
return 0;
return 1;
}
void tarjan(int u)//经典的Tarjan过程
{
int v;
cnt++;
dfn[u]=low[u]=cnt;
instack[u]=1;
S.push(u);
for (int i=0;i<E[u].size();i++)
{
v=E[u][i];
if (dfn[v]==0)
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else
if (instack[v]==1)
low[u]=min(low[u],dfn[v]);
}
if (dfn[u]==low[u])
{
LTKcnt++;
do
{
v=S.top();
S.pop();
instack[v]=0;
Belong[v]=LTKcnt;
}
while (u!=v);
}
return;
}
POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat 二分)的更多相关文章
- POJ 2296 Map Labeler(2-sat)
POJ 2296 Map Labeler 题目链接 题意: 坐标轴上有N个点.要在每一个点上贴一个正方形,这个正方形的横竖边分别和x,y轴平行,而且要使得点要么在正方形的上面那条边的中点,或者在以下那 ...
- POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)
POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...
- POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom /ZOJ 1291 MPI Maelstrom (最短路径)
POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom ...
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
- POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配
两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...
- POJ 2296 Map Labeler (2-Sat)
Map Labeler Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1267 Accepted: 409 Descri ...
- Map Labeler (poj 2296 二分+2-SAT)
Language: Default Map Labeler Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1815 Ac ...
- Map Labeler POJ - 2296(2 - sat 具体关系建边)
题意: 给出n个点 让求这n个点所能建成的正方形的最大边长,要求不覆盖,且这n个点在正方形上或下边的中点位置 解析: 当然是二分,但建图就有点还行..比较难想..行吧...我太垃圾... 2 - s ...
- POJ 2296 Map Labeler
二分答案 + 2-SAT验证,判断正方形是否相交写起来有点烦,思路还是挺简单的. #include<cstdio> #include<cstring> #include< ...
随机推荐
- 【java8】慎用java8的foreach循环
虽然java8出来很久了,但是之前用的一直也不多,最近正好学习了java8,推荐一本书还是不错的<写给大忙人看的javase8>.因为学习了Java8,所以只要能用到的地方都会去用,尤其是 ...
- 6.类似Object监视器方法的Condition接口
在<1.有关线程.并发的基本概念>中,我们利用synchronized关键字.Queue队列.以及Object监视器方法实现了生产者消费者,介绍了有关线程的一些基本概念.Object类提供 ...
- 从零自学Hadoop(23):Impala介绍及安装
阅读目录 序 介绍 安装 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 序 上一篇, ...
- NodeJS在线聊天室(NodeJS & SocketIO & Express & EJS & MongoDB & Gulp)
项目背景 这个项目主要是为了玩玩NodeJS,项目的方向大概是做出类似QQ的在线聊天系统.想要在线体验可以点击在线演示. 项目使用PM2进行部署和管理,功能在不断的迭代开发中.如果你觉得这个项目比较有 ...
- Swoole笔记(一)
简介 Swoole是一个PHP扩展,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,异步Redis,数据库连接池,AsyncTask,消息队列,毫秒定时器,异步文件读 ...
- Mac苹果系统 多系统启动:The rEFInd Boot Manager
苹果系统 多系统启动 下载安装REFIT: 首先安装一下:REFIT, 在这个页面下载: http://refit.sourceforge.net/#download 选择mac disk image ...
- 利用npm安装/删除/发布/更新/撤销发布包 --社会我npm哥,好用话不多
一.什么是npm? npm是javascript的包管理工具,是前端模块化下的一个标志性产物 简单地地说,就是通过npm下载模块,复用已有的代码,提高工作效率 1.从社区的角度:把针对某一特定 ...
- Java代码编写规范(不是标准规范,自行整理,无须纠结)
最近回过头来给以前的项目增加功能,发现之前写的注释非常不全,代码也非常的不整洁,有些地方写的''窝七八烂的,看着很不舒服:又恰好经理最近也经常跟我提起代码规范,我们就讨论了一下代码规范的重要性和必要性 ...
- DiscuzX3.1搬家全过程
最近做了一个安全交流小社区,由于太卡了,之后换了服务器,要给论坛搬家 所以在这里写一下记录. 首先,搬家前要做好以下准备: 1.在网站后台-站长-数据库-备份-网站-Discuz! 和 UCenter ...
- (转)导出EXCEL时科学计数法问题
//1) 文本:vnd.ms-excel.numberformat:@ //2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd //3) 数字:vnd.ms-e ...