我对二分的理解: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. spring web app的结构

    1 入口是web.xml tomcat加载war的时候会去读该入库文件. 2 web.xml中spring mvc的配置 定义servlet到servlet-mapping之间的映射,org.spri ...

  2. 数据处理 数据入数据库 与 Excel

    Python  数据处理   中间数据 Excel   团队交流分工   低的沟通成本    数据入数据库 如postgresql

  3. Linux软件包分类

    源代码包 优点: 1.给你的就是源代码 2.可以修改源代码 3.可以自由选择所需的功能 4.软件是在自己电脑上编译安装,所以更加稳定高效 5.卸载方便(直接删了你安装软件的那个目录就好了) 缺点: 1 ...

  4. Module 'curl' already loaded in Unknown on line 0

    Module 'curl' already loaded in Unknown on line 0 应该是php binary已经包含curl,你又动态加载了一遍.屏蔽掉你的extension 引用, ...

  5. VIM YCM 插件安装问题记录

    参考:https://github.com/yangyangwithgnu/use_vim_as_ide https://github.com/Valloric/YouCompleteMe 根据 ht ...

  6. url信息

    var protocol = window.location.protocol; // "http:" var host = window.location.host; //&qu ...

  7. Chrome Extension 扩展程序 小白入门

    Chrome Extension 扩展程序 前请说明:本文适用于之前从来没有接触过chrome extension扩展程序的同学~ 编写demo 创建项目文件夹chrome_ext_demo,在项目根 ...

  8. poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】

    题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...

  9. linux设置防火墙

    这样其他主机还是无法访问我们的数据库,linux关闭防火墙,其他主机就可以访问了. 关闭防火墙:/etc/init.d/iptables  stop 开启防火墙:/etc/init.d/iptable ...

  10. Oracle数据库操作语言(DML)

    --insert添加语句 insert into table_name(column_name,column_name,...) values (data1,data2,...); --通过表添加数据 ...