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 @   robinyqc  阅读(6)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示