poj3179

二维离散化 二分
check函数有不少需要注意的细节

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;
int c,n;
int len;
int a[505],b[505];
int tmp[1005],s[1005][1005];
bool check(int x)
{
	int pos=lower_bound(tmp+1,tmp+len+1,x)-tmp-1;
	for(int i=pos;i<=len;++i)
	{
		for(int j=pos;j<=len;++j)
		{
			int tx=upper_bound(tmp+1,tmp+i+1,tmp[i]-x)-tmp;
			int ty=upper_bound(tmp+1,tmp+j+1,tmp[j]-x)-tmp;
			if(s[i][j]-s[tx-1][j]-s[i][ty-1]+s[tx-1][ty-1]>=c)return true;
		}
	}
	return false;
}
int main()
{
	scanf("%d%d",&c,&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%d%d",&a[i],&b[i]);
		tmp[i*2-1]=a[i];
		tmp[i*2]=b[i];
	}
	sort(tmp+1,tmp+2*n+1);
	len=unique(tmp+1,tmp+2*n+1)-tmp-1;
	for(int i=1;i<=n;++i)
	{
		int x=lower_bound(tmp+1,tmp+len+1,a[i])-tmp;
		int y=lower_bound(tmp+1,tmp+len+1,b[i])-tmp;
		++s[x][y];
	}
	for(int i=1;i<=len;++i)
	{
		for(int j=1;j<=len;++j)
		{
			s[i][j]+=s[i-1][j]+s[i][j-1]-s[i-1][j-1];
		}
	}
	int l=1,r=tmp[len];
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(check(mid))
		{
			r=mid-1;
		}
		else
		{
			l=mid+1;
		}
	}
	printf("%d\n",l);
	return 0;
}
发布了25 篇原创文章 · 获赞 1 · 访问量 327

猜你喜欢

转载自blog.csdn.net/qq_43665203/article/details/104132829
今日推荐