我对二分的理解:https://www.cnblogs.com/AKMer/p/9737477.html

题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1720

坐标值域很大,但是真正涉及的只有\(500\)个,我们可以离散化做。二分长度,直接二维前缀和检查就行了

h时间复杂度:\(O(log10000*n^2*logn)\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std; int n,c,cnt1,cnt2;
int x[505],y[505],sum[505][505]; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} struct point {
int x,y,x_id,y_id;
}p[505]; bool check(int len) {
for(int i=1;i<=cnt1;i++)
for(int j=1;j<=cnt2;j++) {
int X=upper_bound(x+1,x+cnt1+1,x[i]-len)-x-1;
int Y=upper_bound(y+1,y+cnt2+1,y[j]-len)-y-1;
if(sum[i][j]-sum[i][Y]-sum[X][j]+sum[X][Y]>=c)//二维前缀和
return 1;
}
return 0;
} int main() {
c=read(),n=read();
for(int i=1;i<=n;i++) {
p[i].x=x[i]=read();
p[i].y=y[i]=read();
}
sort(x+1,x+n+1);sort(y+1,y+n+1);
cnt1=unique(x+1,x+n+1)-x-1;
cnt2=unique(y+1,y+n+1)-y-1;
for(int i=1;i<=n;i++) {
p[i].x_id=lower_bound(x+1,x+cnt1+1,p[i].x)-x;
p[i].y_id=lower_bound(y+1,y+cnt2+1,p[i].y)-y;
}
for(int i=1;i<=n;i++)
sum[p[i].x_id][p[i].y_id]++;
for(int i=1;i<=cnt1;i++)
for(int j=1;j<=cnt2;j++)
sum[i][j]=sum[i][j]+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];//离散化,前缀和预处理
int l=0,r=10000;
while(l<r) {
int mid=(l+r)>>1;
if(check(mid))r=mid;
else l=mid+1;//二分,保证所有长度在[r,10000]的正方形可以圈出c块草
}printf("%d\n",r);//最短的那个长度就是r
return 0;
}

BZOJ1720:[Usaco2006 Jan]Corral the Cows 奶牛围栏的更多相关文章

  1. bzoj1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏

    金组题什么的都要绕个弯才能AC..不想银组套模板= = 题目大意:给n个点,求最小边长使得此正方形内的点数不少于c个 首先一看题就知道要二分边长len 本来打算用二维前缀和来判断,显然时间会爆,而且坐 ...

  2. 【BZOJ1720】[Usaco2006 Jan]Corral the Cows 奶牛围栏 双指针法

    [BZOJ1720][Usaco2006 Jan]Corral the Cows 奶牛围栏 Description Farmer John wishes to build a corral for h ...

  3. BZOJ——1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏

    http://www.lydsy.com/JudgeOnline/problem.php?id=1720 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1 ...

  4. bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 -- Tarjan

    1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 Time Limit: 5 Sec  Memory Limit: 64 MB Description The N (2 & ...

  5. 【BZOJ1654】[Usaco2006 Jan]The Cow Prom 奶牛舞会 赤果果的tarjan

    Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in ...

  6. bzoj1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会

    Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in ...

  7. bzoj:1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会

    Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in ...

  8. 【BZOJ】1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会(tarjan)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1654 请不要被这句话误导..“ 如果两只成功跳圆舞的奶牛有绳索相连,那她们可以同属一个组合.” 这句 ...

  9. 【强连通分量】Bzoj1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会

    Description 约翰的N(2≤N≤10000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别上鲜花,她们要表演圆舞.     只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的 ...

随机推荐

  1. javax.servlet.ServletException: Could not resolve view with name‘ XXXX’in servlet with name 'spring'的解决方案-----SKY

    出现的异常如下: javax.servlet.ServletException: Could not resolve view with name '{"msg":"成功 ...

  2. mysql语句, 空的 order by , 空的 where

    SQL语句里, 空的 where, where 1 AND status=1 空的 order by, order by null, updatetime desc 这种空值的情况, 是很有用处的: ...

  3. Django之stark组件的使用和总结

    Stark组件的使用 组件的字段 list_display=[] 需要显示的字段 list_display_links=[] #需要链接编辑字段 Stark_Model_Form=[] #设置Mode ...

  4. python cookbook第三版学习笔记十九:未包装的函数添加参数

    比如有下面如下的代码,每个函数都需要判断debug的是否为True,而默认的debug为False def a(x,debug=False): if debug: print('calling a') ...

  5. Linux c编程:I/O多路复用之epoll

    前面介绍了select处理,这一章继续介绍另外一种I/O多路服用的机制:epoll.来比较下两种机制的不同点. select: 调用过程如下: (1)使用copy_from_user从用户空间拷贝fd ...

  6. Apache Shiro 使用手册(三)Shiro 授权(转发:http://kdboy.iteye.com/blog/1155450)

    授权即访问控制,它将判断用户在应用程序中对资源是否拥有相应的访问权限. 如,判断一个用户有查看页面的权限,编辑数据的权限,拥有某一按钮的权限,以及是否拥有打印的权限等等. 一.授权的三要素 授权有着三 ...

  7. python实例1:创建一个登陆模块

    实现功能: 1.用户输入账户密码 2.验证账户是否存在于黑名单,如果存在于黑名单,则执行1,否则往下执行 3.验证用户名和密码. 3.1.如果验证成功,则打印欢迎信息并退出程序: 3.2.如果用户名存 ...

  8. 数组中去除重复的对象的简单方法and&&的使用

    const arr = [ { name:'tom', age:15 }, { name:'rose', age:17 }, { name:'tom', age:11 }, { name:'rose' ...

  9. 第10条:尽量用enumerate取代range

    核心要点: (1)enumerate函数提供了一种精简的写法,可以在遍历迭代器时获知每个元素的索引. (2)尽量用enumerate来改写那种将range与下标访问相结合的序列遍历代码. (3)可以给 ...

  10. Data Structure Binary Tree: Inorder Tree Traversal without Recursion

    http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/ #include <iostream> #in ...