POJ 2296 Map Labeler(二分边长+2-sat判解)(经典题)

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

不能重叠,求最大的正方形。

经典的题目,找约束关系要经过一些讨论。

//320 KB	16 ms
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 222;
int n;
struct node
{
    int x,y;
}poi[N];
struct edge
{
    int v,next;
}es[N*N];
int head[N],cnt;
inline void add_edge(int u,int v)
{
    es[cnt].v=v;
    es[cnt].next=head[u];
    head[u]=cnt++;
}
int dfn[N],low[N],sta[N],top,tmp[N];
int index;
void tarjan(int u)
{
    dfn[u]=low[u]=++index;
    sta[++top]=u;
    tmp[u]=1;
    for(int i=head[u];~i;i=es[i].next)
    {
        int v=es[i].v;
        if(tmp[v]==0) tarjan(v);
        if(tmp[v]==1) low[u]=min(low[u],low[v]);
    }
    if(dfn[u]==low[u])
    {
        do
        {
            int v=sta[top];
            low[v]=low[u];
            tmp[v]=2;
        }while(sta[top--]!=u);
    }
}
void init()
{
    memset(head,-1,sizeof(head));
    cnt=index=top=0;
    memset(dfn,0,sizeof(dfn));
    memset(tmp,0,sizeof(tmp));
}
bool ok(int r)
{
    init();
    for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++)
        {
            int u=i<<1,v=j<<1;
            if(abs(poi[i].x-poi[j].x)>=r) continue;
            else
            {
                int dy=abs(poi[i].y-poi[j].y);
                if(dy>=2*r) continue;
                else if(dy<2*r&&dy>=r)
                {
                    if(poi[i].y>poi[j].y) add_edge(u^1,v^1),add_edge(v,u);
                    else add_edge(u,v),add_edge(v^1,u^1);
                }
                else if(dy<r)
                {
                    if(poi[i].y>poi[j].y) add_edge(u^1,u),add_edge(v,v^1);
                    else if(poi[i].y<poi[j].y)add_edge(u,u^1),add_edge(v^1,v);
                    else add_edge(u,v^1),add_edge(u^1,v),add_edge(v,u^1),add_edge(v^1,u);
                }
            }
        }
    for(int i=0;i<2*n;i++) if(dfn[i]==0) tarjan(i);
    for(int i=0;i<2*n;i++)
        if(low[i]==low[i^1]) return false;
    return true;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++) scanf("%d%d",&poi[i].x,&poi[i].y);
        int lb=0,ub=100001;
        while(ub-lb>1)
        {
            int mid=(ub+lb)>>1;
            if(ok(mid)) lb=mid;
            else ub=mid;
        }
        printf("%d\n",lb);
    }
    return 0;
}
时间: 2024-08-05 09:21:33

POJ 2296 Map Labeler(二分边长+2-sat判解)(经典题)的相关文章

poj 2296 Map Labeler【二分+2-set】【经典】

题目:poj 2296 Map Labeler 题意:给出以下二维坐标点,然后让你往平面上放正方形,点必须落在正方形上面边的中点或者下面边的中点,正方形不能重叠,可以共用边.问最大正方形边的边长. 分析:这种最大化最小值或者最小化最大值的问题,我们都可以种二分+判断的方法来解,这个也不例外,关键是判断部分 我们现在二分枚举边长为diff,然后所有的点就变成了在正方形上面或者下面的问题了,二选一的问题很明显可以用2-set来判断的 这个题目的关键在于理解他们之间的二元关系并建图,下面我们来分析 首

POJ 2296 Map Labeler(2-sat)

POJ 2296 Map Labeler 题目链接 题意: 坐标轴上有N个点,要在每个点上贴一个正方形,这个正方形的横竖边分别和x,y轴平行,并且要使得点要么在正方形的上面那条边的中点,或者在下面那条边的中点,并且任意两个点的正方形都不重叠(可以重边).问正方形最大边长可以多少? 思路:显然的2-sat问题,注意判断两个矩形相交的地方,细节 代码: #include <cstdio> #include <cstring> #include <cstdlib> #incl

POJ 2296 Map Labeler

二分答案 + 2-SAT验证,判断正方形是否相交写起来有点烦,思路还是挺简单的. #include<cstdio> #include<cstring> #include<cmath> #include<stack> #include<queue> #include<algorithm> using namespace std; const int maxn=1005; struct Point { double x,y; }p[max

POJ 2411 &amp;&amp; HDU 1400 Mondriaan&#39;s Dream (状压dp 经典题)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12341   Accepted: 7204 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

Map Labeler (poj 2296 二分+2-SAT)

Language: Default Map Labeler Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1815   Accepted: 599 Description Map generation is a difficult task in cartography. A vital part of such task is automatic labeling of the cities in a map; whe

POJ 2263 Heavy Cargo(二分+并查集)

题目地址:POJ 2263 这题是在网上的一篇关于优先队列的博文中看到的..但是实在没看出跟优先队列有什么关系..我用的二分+并查集做出来了... 二分路的载重量.然后用并查集检查是否连通. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #

poj 3977 Subset 枚举+二分

首先分成一半2^17和2^18,并且把其中一半变成相反数,然后枚举一半二分查找另一半,在找到的位置前后也找找. 这里用到了二级排序,有很多细节要处理,不多说了. 巨坑的一个地方就是,不能用系统的abs,要自己手写,简直坑死.. #include<cstdio> #include<algorithm> #include<iostream> #include<map> using namespace std; typedef long long ll; stru

POJ 2112 Optimal Milking 二分答案+最大流

首先二分最长的边,然后删去所有比当前枚举的值长的边,算最大流,看是否能满足所有的牛都能找到挤奶的地方 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include

POJ 3228Gold Transportation(二分+最大流)

题目地址:POJ3288 这个题跟之前的一道题混了,感觉是一样的,所以连想都没怎么想就拆点然后求最短路然后二分求最大流了.结果连样例都不过,还一直以为又是哪里手残了..结果看了看样例,手算也确实不对,,.后来就没拆点,直接在原点上建的图,结果样例就过了..然后提交1次AC... 后来仔细的想了想,这题是问每一段路的最短的,而不是整个的最短的.所以说不应该拆点.而我混的那道题(poj2391)是问的走完的总的最小值. 这题的建图就是建一源点与汇点,将每个金矿与源点相连,权值为金矿数,与汇点也相连,