POJ 2296 Map Labeler 2-SAT 二分

21 篇文章 0 订阅
11 篇文章 0 订阅

有n个点,只能位于某个正方形的上或下边的中点上,所有正方形边长相等且不重叠,求最大边长。
二分答案。
如果某两个点的x坐标不小于边长就无所谓。
如果小于,就得考虑在上面下面的问题。
如果y坐标小于边长,那么就必须一个放上一个放下;
如果相等,一个上一个中,一个中一个下;
如果小于两倍边长,那么上、中上、中下、下。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define rep(i,j,k) for(i=j;i<k;++i)
#define ms(i) memset(i,0,sizeof(i))
const int N = 20005, M = 600000;
int x[N], y[N], dfn[N], low[N], belong[N];
int h[N], p[M], v[M], vis[N], stk[N], top, ts, cnt, scc;
void add(int a, int b) {
    p[++cnt] = h[a]; v[cnt] = b; h[a] = cnt;
}

void tarjan(int x) {
    int i;
    dfn[x] = low[x] = ++ts;
    vis[x] = 1; stk[++top] = x;
    for (i = h[x]; i; i = p[i])
        if (!dfn[v[i]]) {
            tarjan(v[i]);
            low[x] = min(low[v[i]], low[x]);
        } else if (vis[v[i]])
            low[x] = min(dfn[v[i]], low[x]);
    if (dfn[x] == low[x]) {
        ++scc;
        do {
            i = stk[top--];
            vis[i] = 0;
            belong[i] = scc;
        } while (i != x);
    }
}

int main() {
    int n, m, i, j, t, kase = 0, l, r, mid, ans;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        rep(i,0,n) scanf("%d%d", x + i, y + i);
        int l = 0, r = 40000;
        while (l <= r) {
            mid = l + r >> 1;
            ms(h); ms(dfn); scc = cnt = top = ts = 0;
            rep(i,0,n) rep(j,i+1,n) if (abs(x[i] - x[j]) < mid) {
                if (y[i] == y[j]) add(i,j+n),add(j,i+n),add(j+n,i),add(i+n,j);
                else if (y[i] > y[j] && y[i] - y[j] < mid) add(i,i+n),add(j+n,j);
                else if (y[i] < y[j] && y[j] - y[i] < mid) add(j,j+n),add(i+n,i);
                else if (y[i] > y[j] && y[i] - y[j] < 2 * mid) add(i,j),add(i+n,i+n);
                else if (y[i] < y[j] && y[j] - y[i] < 2 * mid) add(j,i),add(i+n,j+n);
            }
            rep(i,0,2*n) if (!dfn[i]) tarjan(i);
            bool flag = true;
            rep(i,0,n) if (belong[i] == belong[i + n])
            { flag = false; break; }
            if (flag) l = mid + 1, ans = mid;
            else r = mid - 1;
        }
        printf("%d\n", ans);
    }
    return 0;
}

Map Labeler

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1902 Accepted: 626

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值