POJ 2296 Map Labeler (2-Sat)

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

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

Source

题意:给你n个点,要你在这n个点上方一个正方形,点只能在正方形的上边或下边的中点上,所有正方形大小一样,不能重叠,求最大的正方形

题型:二分+可行性判断。

二分+2-SAT可行性判断
题意:给你n个点,要你在这n个点上放一个正方形,点
只能在正方形的上边或下边的中点上,所有正方形大小一样,
不能重叠,求最大的正方形。。。

如果abs(s[i].x-s[j].x)>=r则可以随便放
如果 abs[s[i].x-s[j].y)<r;
   如果abs(s[i].y-s[j].y)<r,如果s[i].y==s[i].y则要求一个放上面一个放下面。
                            否则只能是上面的点放上面,下面的点放下面。
   如果r<=abs(s[i].y-s[j].y)<2*r,则除了上面的点放下方、下面的点放上方的情况都是可以的。

 
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int VM=2100;
const int EM=400100;
const double eps=1e-8;

struct Edge{
    int to,nxt;
}edge[EM<<1];

int n,m,cnt,dep,top,atype,head[VM];
int dfn[VM],low[VM],vis[VM],belong[VM];
int stack[VM],x[VM],y[VM];

void Init(){
    cnt=0,  atype=0,    dep=0,  top=0;
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(belong,0,sizeof(belong));
}

void addedge(int cu,int cv){
    edge[cnt].to=cv;    edge[cnt].nxt=head[cu];     head[cu]=cnt++;
}

void Tarjan(int u){
    dfn[u]=low[u]=++dep;
    stack[top++]=u;
    vis[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].nxt){
        int v=edge[i].to;
        if(!dfn[v]){
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }else if(vis[v])
            low[u]=min(low[u],dfn[v]);
    }
    int j;
    if(dfn[u]==low[u]){
        atype++;
        do{
            j=stack[--top];
            belong[j]=atype;
            vis[j]=0;
        }while(u!=j);
    }
}

int abs(int x){
    return x<0?-x:x;
}

int solve(int mid){
    for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++)
            if(abs(x[i]-x[j])<mid && abs(y[i]-y[j])<2*mid){
                if(abs(y[i]-y[j])>=mid){
                    if(y[i]<y[j]){
                        addedge(2*i+1,2*j+1);
                        addedge(2*j,2*i);
                    }else{
                        addedge(2*i,2*j);
                        addedge(2*j+1,2*i+1);
                    }
                }else if(y[i]<y[j]){
                    addedge(2*i+1,2*i);
                    addedge(2*j,2*j+1);
                }else if(y[i]>y[j]){
                    addedge(2*i,2*i+1);
                    addedge(2*j+1,2*j);
                }else{
                    addedge(2*i+1,2*j);
                    addedge(2*i,2*j+1);
                    addedge(2*j,2*i+1);
                    addedge(2*j+1,2*i);
                }
            }

    for(int i=0;i<2*n;i++)
        if(!dfn[i])
            Tarjan(i);
    for(int i=0;i<2*n;i+=2)
        if(belong[i]==belong[i^1])
            return 0;
    return 1;
}

int main(){

    //freopen("input.txt","r",stdin);

    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d%d",&x[i],&y[i]);
        int l=0,r=100000,mid,ans=0;
        while(l<=r){
            Init();     //因为二分每次都得重新建边,所以初始化在这里
            mid=(l+r)>>1;
            if(solve(mid)){ //return  true,说明边太少了,应该增大mid,所以l = mid
                l=mid+1;
                ans=mid;
            }else    //return  false,说明边太多了,应该减小mid,所以r = mid
                r=mid-1;
        }
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jackge/p/3180765.html