bzoj 1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏 二分

版权声明:本文为博主原创文章,转载请联系博主。 https://blog.csdn.net/everlasting_20141622/article/details/77214230

题目链接


先按奶牛的横坐标排序,然后二分答案

判断圈住多少奶牛暴力搞就好,N<=500怎么搞都能过


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>

#define inf 999999999

using namespace std;

struct point{
	int x,y;
	friend bool operator < (point a,point b){
		if(a.x==b.x)return a.y<b.y;
		return a.x<b.x;
	}
};

point a[550];
int n,m;
int ans;

bool check(int x){
	int sum=0;
	for(int i=0; i<n; i++){
		int j=i;
		while(j<n && a[j].x-a[i].x<=x-1)j++;
		j--;
		if(j-i+1<m)return false;
		for(int k=i; k<=j; k++){
			int l=a[k].y,r=a[k].y+x-1;
			int sum=0;
			for(int u=i; u<=j; u++){
				if(a[u].y>=l && a[u].y<=r)sum++;
			}
			if(sum>=m)return true;
		}
	}
	return false;
}

int main(){
	scanf("%d%d",&m,&n);
	for(int i=0; i<n; i++)scanf("%d%d",&a[i].x,&a[i].y);
	sort(a,a+n);
	int l=0,r=10000;
	while(l<=r){
		int mid=(l+r)/2;
		if(check(mid))r=mid-1,ans=mid;
		else l=mid+1;
	}
	printf("%d\n",ans);
	
	return 0;
}


没有更多推荐了,返回首页