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
Solution
题目要求最大值最小,很容易想到二分。
判断时,对于每一个点,有上标签与下标签两种选择必选且只能选其一,同时某些标签会与某些标签冲突,所以想到2-SAT
code
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define mid ((l + r) >> 1)
#define other(x) ((x) % 2 ? (x) + 1 : (x) - 1)
#define ok(x) (x == -1 ? 0 : -1)
const int maxn = 2 * 105;
struct point {
int x, y;
}p[maxn];
struct edge {
int to, next;
}e[maxn * maxn * 2];
int tot, h[maxn];
int x, n;
void add(int u, int v);
inline bool xj(int a, int i, int b, int j);
void build_g();
int Time, dfn[maxn], low[maxn];
int top, stack[maxn];
bool vis[maxn];
int cnt, group[maxn];
inline void chkmin(int &a, int b)
{
if(a > b) a = b;
}
void output(int u)
{
cnt++;
while(stack[top] != u) {
group[stack[top]] = cnt;
vis[stack[top--]] = false;
}
group[stack[top]] = cnt;
vis[stack[top--]] = false;
}
void tarjan(int u)
{
dfn[u] = low[u] = ++Time;
stack[++top] = u;
vis[u] = true;
for(int i = h[u]; i; i = e[i].next)
if(!dfn[e[i].to]) {
tarjan(e[i].to);
chkmin(low[u], low[e[i].to]);
}else if(vis[e[i].to]) chkmin(low[u], dfn[e[i].to]);
if(dfn[u] == low[u]) output(u);
}
bool check()
{
memset(h, 0, sizeof(h));
//memset(stack, 0, sizeof(stack));
//memset(group, 0, sizeof(group));
Time = tot = cnt = 0;
memset(dfn, 0, sizeof(dfn));
build_g();
for(int i = 1; i <= 2 * n; ++i)
if(!dfn[i]) tarjan(i);
for(int i = 1; i <= n; ++i)
if(group[i * 2 - 1] == group[i * 2]) return false;
return true;
}
int main()
{
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
int T;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
scanf("%d%d", &p[i].x, &p[i].y);
int l = 1, r = 10000;
do {
x = mid;
if(check()) l = mid + 1;
else r = mid - 1;
}while(l <= r);
printf("%d\n", l - 1);
}
return 0;
}
inline void add(int u, int v)
{
e[++tot] = (edge) {v, h[u]};
h[u] = tot;
//if(x == 5000) printf("%d %d\n", u, v);
}
inline bool xj(int a, int i, int b, int j)
{
double x1 = p[a].x, x2 = p[b].x;
double y1 = (double)p[a].y + (double)i * x/2., y2 = (double)p[b].y + (double)j * x/2.;
if(max(x1 - x/2., x2 - x/2.) < min(x1 + x/2., x2 + x/2.)
&&max(y1 - x/2., y2 - x/2.) < min(y1 + x/2., y2 + x/2.)) return true;
return false;
}
void build_g()
{
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
for(int k = -1; k <= 1; k += 2)
for(int l = -1; l <= 1; l += 2)
if(i != j && xj(i, k, j, l)) add(i * 2 + ok(k), other(j * 2 + ok(l))),
add(j * 2 + ok(l), other(i * 2 + ok(k)));
}