[USACO06JAN]把牛Corral the Cows
# #### 蒟蒻第一次发题解(~~外加第一次用MARKDOWN~~)
一眼就能看出的二分题
主函数中二分枚举答案,
判断时每次将合理x轴范围p[i].x到p[i].x+qh-1内所有点按照p[i].y从小到大排序,再以要求的个数分组判断是否在区间内
核心代码
### # if(p[sb[j]].y-p[sb[j-c+1]].y+1<=qh) return 1;
以下是代码
```
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
#include<cstring>
using namespace std;
const double inf = 1e10;
const int maxn = 0x7f7f7f7f;
inline void read(int &x)
{
x=0;int f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
while(c<='9'&&c>='0'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
x*=f;
}
inline void print(int x)
{
if(x<0){x=-x;putchar('-');}
if(x>9) print(x/10);
putchar(x%10+'0');
}
struct po{
int x,y;
}p[555];
int c,n;
int sb[501];
inline bool mmp(int a,int b)
{
return p[a].y<p[b].y;
}
inline int check(int qh)
{
for(register int i=1;i<=n;i++)
{
int tot=0,maxx=qh+p[i].x-1;
for(register int j=i;j<=n;j++)
{
if(p[j].x<=maxx)
{
tot++;
sb[tot]=j;
}
else break;
}
if(tot<c) continue;
stable_sort(sb+1,sb+tot+1,mmp);
for(int j=tot;j>=c;j--)
if(p[sb[j]].y-p[sb[j-c+1]].y+1<=qh) return 1;
}
return 0;
}
inline bool cmp(po a,po b)
{
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
int main()
{
int zuida=-maxn,zuixiao=maxn;
read(c);read(n);
for(register int ii=1;ii<=n;ii++)
{
read(p[ii].x);
read(p[ii].y);
}
stable_sort(p+1,p+n+1,cmp);
int l=1,r=10001,ans=0,temp=-1,mid=-1;
for(int ii=1;ii<=100;ii++)
{
temp=mid;
mid=(l+r)>>1;
if(temp==mid) break;
if(check(mid))
{
ans=mid;
r=mid;
}
else l=mid;
}
print(ans);
}
```