我对二分的理解: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. Redis主从、事务、哨兵、消息、代理分片

    安装 wget http://download.redis.io/releases/redis-3.0.5.tar.gz tar -zxvf redis-3.0.5.tar.gz cd redis-3 ...

  2. JDK动态代理连接池

    JDK动态代理   1 什么是JDK动态代理 刚刚写ItcastConnection时爽么?因为Connection中的方法太多了,每个都要写,所以很累吧.累点到是没什么,可以完成功能就是好的.但是不 ...

  3. linux c编程:进程控制(三)_exec函数

    fork()函数通过系统调用创建一个与原来进程(父进程)几乎完全相同的进程(子进程是父进程的副本,它将获得父进程数据空间.堆.栈等资源的副本.注意,子进程持有的是上述存储空间的“副本”,这意味着父子进 ...

  4. The given 'driver' ] is unknown, Doctrine currently supports only the follo wing drivers: pdo_mysql, pdo_sqlite, pdo_pgsql, pdo_oci, oci8, ibm_db2, pdo

    [Doctrine\DBAL\DBALException]                                                  The given 'driver' ] ...

  5. linux 9 -- 交互式使用Bash Shell

    二十二. 交互式使用Bash Shell:     1.  用set命令设置bash的选项:     下面为set主要选项的列表及其表述: 选项名 开关缩写 描述 allexport -a 打开此开关 ...

  6. HDU - 1728 逃离迷宫 【BFS】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1728 思路 BFS 一开始 从开始位置 往四周走 如果能走的话 这个时候 转弯次数都是0 我们的标记不 ...

  7. Swift 闭包的简单学习

    OC中已经学习了闭包 在swift里面 该怎么处理 不多说 上代码 //(num:Int) ->Bool是闭包的参数类型 func hasCloserMatch(arr :[Int], valu ...

  8. iOS swift 语句只能写在函数体内

    1. 语句只能在函数体内: eg    因为我写在playground里面没报错  我直接放在这个位置就报错了 在这个.swift 文件里面 print 应该写在func 等方法(函数)里面 其他语句 ...

  9. Data Structure Linked List: Merge Sort for Linked Lists

    http://www.geeksforgeeks.org/merge-sort-for-linked-list/ #include <iostream> #include <vect ...

  10. 最新版本express 4.2.0的快速建站--简单举例

    express是Node.js的官方唯一推荐的框架,它扩展了http工具以及一些好用的功能.如此好用的东西,赶紧的安装起来: npm install express -g 新版本的express的命令 ...