暴搜 bzoj1052 覆盖问题

Description

Input

Output

Sample Input
4
0 1
0 -1
1 0
-1 0
Sample Output
1
HINT
100%的数据，N<=20000

#pragma GCC optimize("O3")
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 20005
#define inf 100000000
#define ll long long
using namespace std;
{
int sum=0,f=1;char x=getchar();
while(x<'0'||x>'9'){if(x=='-')f=-1;x=getchar();}
while(x>='0'&&x<='9'){sum=(sum<<1)+(sum<<3)+x-'0';x=getchar();}
return sum*f;
}
struct node{ll x,y;}a[N];
int n;
bool vis[N];
ll hx=inf,hd=-inf,zx=inf,zd=-inf;
inline bool cmp(node a,node b){return a.x==b.x?a.y<b.y:a.x<b.x;}
inline int check(ll h1,ll h2,ll z1,ll z2,ll x,int h)
{
if(h==4)return 0;
ll f1=inf,f2=-inf,g1=inf,g2=-inf;int hh[N];memcpy(hh,vis,sizeof(vis));
for(int i=1;i<=n;i++)if(!vis[i]&&a[i].x<=h1+x&&a[i].y<=z1+x)vis[i]=1;
int s=0;for(int i=1;i<=n;i++)if(vis[i])s++;if(s==n)return 1;
for(int i=1;i<=n;i++)
{
if(vis[i])continue;
if(a[i].x>f2)f2=a[i].x;
if(a[i].x<f1)f1=a[i].x;
if(a[i].y>g2)g2=a[i].y;
if(a[i].y<g1)g1=a[i].y;
}
int k=check(f1,f2,g1,g2,x,h+1);if(k==1)return 1;
memcpy(vis,hh,sizeof(vis));

f1=inf,f2=-inf,g1=inf,g2=-inf;memcpy(hh,vis,sizeof(vis));
for(int i=1;i<=n;i++)if(!vis[i]&&a[i].x<=h1+x&&a[i].y>=z2-x)vis[i]=1;
s=0;for(int i=1;i<=n;i++)if(vis[i])s++;if(s==n)return 1;
for(int i=1;i<=n;i++)
{
if(vis[i])continue;
if(a[i].x>f2)f2=a[i].x;
if(a[i].x<f1)f1=a[i].x;
if(a[i].y>g2)g2=a[i].y;
if(a[i].y<g1)g1=a[i].y;
}
k=check(f1,f2,g1,g2,x,h+1);if(k==1)return 1;
memcpy(vis,hh,sizeof(vis));

f1=inf,f2=-inf,g1=inf,g2=-inf;memcpy(hh,vis,sizeof(vis));
for(int i=1;i<=n;i++)if(!vis[i]&&a[i].x>=h2-x&&a[i].y<=z1+x)vis[i]=1;
s=0;for(int i=1;i<=n;i++)if(vis[i])s++;if(s==n)return 1;
for(int i=1;i<=n;i++)
{
if(vis[i])continue;
if(a[i].x>f2)f2=a[i].x;
if(a[i].x<f1)f1=a[i].x;
if(a[i].y>g2)g2=a[i].y;
if(a[i].y<g1)g1=a[i].y;
}
k=check(f1,f2,g1,g2,x,h+1);if(k==1)return 1;
memcpy(vis,hh,sizeof(vis));

f1=inf,f2=-inf,g1=inf,g2=-inf;memcpy(hh,vis,sizeof(vis));
for(int i=1;i<=n;i++)if(!vis[i]&&a[i].x>=h2-x&&a[i].y>=z2-x)vis[i]=1;
s=0;for(int i=1;i<=n;i++)if(vis[i])s++;if(s==n)return 1;
for(int i=1;i<=n;i++)
{
if(vis[i])continue;
if(a[i].x>f2)f2=a[i].x;
if(a[i].x<f1)f1=a[i].x;
if(a[i].y>g2)g2=a[i].y;
if(a[i].y<g1)g1=a[i].y;
}
k=check(f1,f2,g1,g2,x,h+1);if(k==1)return 1;
memcpy(vis,hh,sizeof(vis));
return 0;
}
int main()
{
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++)
{
if(a[i].x>hd)hd=a[i].x;
if(a[i].x<hx)hx=a[i].x;
if(a[i].y>zd)zd=a[i].y;
if(a[i].y<zx)zx=a[i].y;
}
ll l=0,r=max(zd-zx,hd-hx),mid,ans;
while(l<=r)
{
mid=(l+r)/2;
memset(vis,0,sizeof(vis));
if(check(hx,hd,zx,zd,mid,1))ans=mid,r=mid-1;
else l=mid+1;
}
cout<<ans;
}
posted @ 2017-10-06 20:19  Hzoi_QTY  阅读(82)  评论(0编辑  收藏