我对二分的理解: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. And Then There Was One(约瑟夫环)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  2. WebApi 中使用 Token

    1.登陆的时候根据用户信息生成Token var token = FormsAuthentication.Encrypt( new FormsAuthenticationTicket( , " ...

  3. vue v-on命令

    <!-- 阻止单击事件冒泡 --> <a v-on:click.stop="doThis"></a>   <!-- 提交事件不再重载页面 ...

  4. ShowModal 代码分析

    下面为Delphi中,方法TCustomForm.ShowModal的代码,通过分析以下代码,可以了解ShowModal到底是怎么一回事! 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  5. 7.Django模型类的定义和管理

    Django的模型类是给ORM层服务的 1.每个数据模型都是django.db.models.Model的子类. 2.它的父类Model包含了所有必要的和数据库交互的方法,并提供了定义数据库字段的语法 ...

  6. linux c编程:线程互斥一

    当多个线程共享相同的内存的时候,需要确保每个线程都看到一致的数据视图.如果每个线程使用的变量都是其他线程不会读取和修改的.那么就不存在一致性问题.同样,如果变量是只读的,多个线程也不会有一致性的问题. ...

  7. PHP实现今天是星期几的几种写法

    今天是星期几的写法有很多,本文整理了常用的三种. 代码如下:  // 第一种写法 $da = date("w");  if( $da == "1" ){  ec ...

  8. iOS category 类别 和 extension 扩展

    category  类别 又称为 分类 在ios项目开发中允许使用类别为现有的类添加新的方法,并不需要创建子类.通过类别我们可以动态地为现有的类添加新的方法,可以将类的定义模块化地布局到多个相关文件中 ...

  9. 【LeetCode】【数组归并】Merge k Sorted Lists

    描述 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  10. python cookbook 数据结构

    保留最后n个元素: from collections import deque def search (lines, pattern, history=): previous_lines = dequ ...