# BZOJ1052 [HAOI2007]覆盖问题

## 代码实现

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi(){
int f=1,sum=0;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
const int inf=1e9;
struct data{int x[20010],y[20010],top;};
int mid;
void cut(data &a,int x1,int y1,int x2,int y2){
int tot=0;
for(int i=1;i<=a.top;i++)
if(a.x[i]<x1||a.x[i]>x2||a.y[i]<y1||a.y[i]>y2)
{
tot++;
a.x[tot]=a.x[i];
a.y[tot]=a.y[i];
}
a.top=tot;
}
void solve(data &a,int fc)
{
int x1=inf,y1=inf,x2=-inf,y2=-inf;
for(int i=1;i<=a.top;i++)
{
x1=min(a.x[i],x1),x2=max(a.x[i],x2);
y1=min(a.y[i],y1),y2=max(a.y[i],y2);
}
if(fc==1)
cut(a,x1,y1,x1+mid,y1+mid);
if(fc==2)
cut(a,x2-mid,y1,x2,y1+mid);
if(fc==3)
cut(a,x1,y2-mid,x1+mid,y2);
if(fc==4)
cut(a,x2-mid,y2-mid,x2,y2);
}
data a;
bool jud()
{
data b;
for(int x=1;x<=4;x++)
for(int y=1;y<=4;y++)
{
b.top=a.top;
for(int i=1;i<=b.top;i++)
b.x[i]=a.x[i],b.y[i]=a.y[i];
solve(b,x);solve(b,y);
int x1=inf,y1=inf,x2=-inf,y2=-inf;
for(int i=1;i<=b.top;i++)
{
x1=min(b.x[i],x1),x2=max(b.x[i],x2);
y1=min(b.y[i],y1),y2=max(b.y[i],y2);
}
if(x2-x1<=mid&&y2-y1<=mid)return 1;
}
return 0;
}
int main(){
int n=gi();
for(int i=1;i<=n;i++)a.x[i]=gi(),a.y[i]=gi();
a.top=n;
int l=1,r=inf,ans=0;
while(l<=r){
mid=(l+r)>>1;
if(jud()){r=mid-1;ans=mid;}
else l=mid+1;
}
printf("%d\n",ans);
return 0;
}

posted @ 2019-01-24 10:37  QwQGJH  阅读(116)  评论(0编辑  收藏  举报