ZSOI2012 最大立方体空间 二分答案

并不会正解,正解是用segtree实现的KDtree

二分答案,然后暴力判断

有80分诶!

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<utility>
#define LL long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define efo(i,x) for(int i=last[x];i!=0;i=e[i].next)
using namespace std;
inline LL read()
{
	LL d=0,f=1;char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
	while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
	return d*f;
}
#define N 1005
typedef struct node
{
	int x1,y1,z1;
	int x2,y2,z2;
	void BOX(int x,int y,int z,int p,int q,int w)
	{
		x1=x,y1=y,z1=z;
		x2=p,y2=q,z2=w;
	}
	void check()
	{
		printf("%d %d %d %d %d %d\n",x1,y1,z1,x2,y2,z2);
	}
}BOX;
BOX cube[N];
int n;
int MX,MY,MZ;
bool flag;
int tt=0;

void SearchSp(int x,int y,int z,int now,int L)
{
	if(flag)return;
	int xx=x+L,yy=y+L,zz=z+L;
	if(xx>MX||yy>MY||zz>MZ)return;
	tt++;
	if(tt>27000000)return ;
	if(now>n)
	{
		flag=1;
		return;
	}
//	cout<<x<<' '<<y<<' '<<z<<' '<<now<<' '<<L<<endl;
	if(xx<=cube[now].x1||yy<=cube[now].y1||zz<=cube[now].z1||x>=cube[now].x2||y>=cube[now].y2||z>=cube[now].z2)
	{
		SearchSp(x,y,z,now+1,L);
		return;
	}
	SearchSp(cube[now].x2 , y , z , 1 , L);
	SearchSp(x , cube[now].y2 , z , 1 , L);
	SearchSp(x , y , cube[now].z2 , 1 , L);
}

bool check(int anss)
{
	flag=0;
	SearchSp(0,0,0,1,anss);
	if(flag)return 1;
	return 0;
}

int EF(int l,int r)
{
	int ret=-1;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(check(mid))
		{
			ret=mid;
			l=mid+1;
		}else r=mid-1;
		if(n>=500&&clock()-tt>0.100)break;
	}
	return ret;
}

int main()
{
	freopen("cube.in","r",stdin);
	freopen("cube.out","w",stdout);
	int T=read(),tot=0;
	while(tot++!=T)
	{
		n=read(),MX=read(),MY=read(),MZ=read();
		fo(i,1,n)
		{
			int x=read(),y=read(),z=read();
			int p=read(),q=read(),w=read();
			cube[i].BOX(x,y,z,p,q,w);
		}
		int ans=EF(0,min(MX,min(MY,MZ)));
		cout<<"Case "<<tot<<": "<<ans<<endl;
	}
	return 0;
}


你可能感兴趣的:(暴力,二分答案)