LuoguP2862 [USACO06JAN]Corral the Cows G

思路可以看 yxc 的视频 link

这里采用了偷懒的离散化思想,即把 x,y 合并起来考虑。如果分离,效率更高。

code:

#include<bits/stdc++.h>
using namespace std;
int c,n,x[505],y[505],sum[1005][1005];
vector<int> h={0};
int Get(int num){return lower_bound(h.begin(),h.end(),num)-h.begin();}
bool check(int l){
	for(int x1=1,x2=1,len=h.size();x2<len;x2++){
		while(h[x2]-h[x1]+1>l)x1++;
		for(int y1=1,y2=1;y2<len;y2++){
			while(h[y2]-h[y1]+1>l)y1++;
			if(sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1]>=c)
				return true;
		}
	}return false;
}
signed main(){
	cin>>c>>n;
	for(int i=1;i<=n;i++)//这里采用偷懒写法,效率降低不少QAQ
		cin>>x[i]>>y[i],h.push_back(x[i]),h.push_back(y[i]);
	sort(h.begin(),h.end());//unique 只针对相邻元素,故先排序
	h.erase(unique(h.begin(),h.end()),h.end());
	for(int i=1;i<=n;i++)
		sum[Get(x[i])][Get(y[i])]++;
	for(int i=1;i<=1000;i++)
		for(int j=1;j<=1000;j++)
			sum[i][j]+=(sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]);
	int l=1,r=10000,mid;
	while(l<r){
		mid=l+((r-l)>>1);
		if(check(mid))r=mid;
		else l=mid+1;
	}cout<<r;
	return 0;
}

posted @ 2022-11-12 11:00  robinyqc  阅读(14)  评论(0)    收藏  举报