【BZOJ1720】[Usaco2006 Jan]Corral the Cows 奶牛围栏 双指针法
【BZOJ1720】[Usaco2006 Jan]Corral the Cows 奶牛围栏
Description
Farmer John wishes to build a corral for his cows. Being finicky beasts, they demand that the corral be square and that the corral contain at least C (1 <= C <= 500) clover fields for afternoon treats. The corral's edges must be parallel to the X,Y axes. FJ's land contains a total of N (C <= N <= 500) clover fields, each a block of size 1 x 1 and located at with its lower left corner at integer X and Y coordinates each in the range 1..10,000. Sometimes more than one clover field grows at the same location; such a field would have its location appear twice (or more) in the input. A corral surrounds a clover field if the field is entirely located inside the corral's borders. Help FJ by telling him the side length of the smallest square containing C clover fields.
Input
* Line 1: Two space-separated integers: C and N
* Lines 2..N+1: Each line contains two space-separated integers that are the X,Y coordinates of a clover field.
Output
* Line 1: A single line with a single integer that is length of one edge of the minimum size square that contains at least C clover fields.
Sample Input
1 2
2 1
4 1
5 2
Sample Output
OUTPUT DETAILS:
Below is one 4x4 solution (C's show most of the corral's area); many
others exist.
|CCCC
|CCCC
|*CCC*
|C*C*
+------
题解:二维双指针法的完美结合
双指针法就是令l=1,从1到n枚举右指针r,然后始终保证[l,r]的区间是满足题目要求的区间,不满足就使l++,并每次用[l,r]更新答案(感觉就是简化的单调队列)
如果坐标都是一维的,我们只用双指针法就能搞定,但是由于是二维的,所以我们要用两重双指针法(四指针法。。。)
先二分答案,分别用双指针法维护纵坐标和横坐标,具体还是看代码吧~
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n,m,nx,ny;
struct field
{
int x,y;
}p[510];
int rx[510],ry[510],s[510];
bool cmp1(field a,field b)
{
return a.x<b.x;
}
bool cmp2(field a,field b)
{
return a.y<b.y;
}
bool solve(int ml)
{
int i,a,b,c,d,sc,sd;
a=b=0;
memset(s,0,sizeof(s));
while(b<n&&rx[p[b+1].x]-rx[1]+1<=ml) s[p[++b].y]++;
for(;b<=n;s[p[++b].y]++)
{
while(rx[p[b].x]-rx[p[a+1].x]+1>ml) s[p[++a].y]--;
c=d=sc=sd=0;
while(d<ny&&ry[d+1]-ry[1]+1<=ml) sd+=s[++d];
for(;d<=ny;sd+=s[++d])
{
while(ry[d]-ry[c+1]+1>ml) sc+=s[++c];
if(sd-sc>=m) return true;
}
}
return false;
}
int main()
{
scanf("%d%d",&m,&n);
int i;
rx[0]=ry[0]=-1;
for(i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
sort(p+1,p+n+1,cmp2);
for(i=1;i<=n;i++)
{
if(p[i].y>ry[ny]) ry[++ny]=p[i].y;
p[i].y=ny;
}
sort(p+1,p+n+1,cmp1);
for(i=1;i<=n;i++)
{
if(p[i].x>rx[nx]) rx[++nx]=p[i].x;
p[i].x=nx;
}
int l=1,r=max(rx[nx],ry[ny]),mid;
while(l<r)
{
mid=l+r>>1;
if(solve(mid)) r=mid;
else l=mid+1;
}
printf("%d",r);
return 0;
}
【BZOJ1720】[Usaco2006 Jan]Corral the Cows 奶牛围栏 双指针法的更多相关文章
- bzoj1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏
金组题什么的都要绕个弯才能AC..不想银组套模板= = 题目大意:给n个点,求最小边长使得此正方形内的点数不少于c个 首先一看题就知道要二分边长len 本来打算用二维前缀和来判断,显然时间会爆,而且坐 ...
- BZOJ1720:[Usaco2006 Jan]Corral the Cows 奶牛围栏
我对二分的理解:https://www.cnblogs.com/AKMer/p/9737477.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...
- 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 ...
- 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 & ...
- 【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 ...
- 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 ...
- 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 ...
- 【BZOJ】1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会(tarjan)
http://www.lydsy.com/JudgeOnline/problem.php?id=1654 请不要被这句话误导..“ 如果两只成功跳圆舞的奶牛有绳索相连,那她们可以同属一个组合.” 这句 ...
- 【强连通分量】Bzoj1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会
Description 约翰的N(2≤N≤10000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别上鲜花,她们要表演圆舞. 只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的 ...
随机推荐
- CDH离线安装
1. 安装准备 系统:Centos 6 Cloudera Manager分配如下: 安装版本:CDH-5.8.0 所需安装文件 CDH相关 CDH-5.8.0-1.cdh5.8.0.p0.42-el6 ...
- [android] Android 错误集锦
问题1:导入工程时报错The import android.XXX cannot be resolved 解决方法: 1.右键工程→Bulid Path→Configure Build Path... ...
- 为什么要把session存入数据库
比如网易的通行证,一个session能进入很多的网易下的网站
- Struts2学习笔记(OGNL表达式)
Struts 2支持以下几种表达式语言: OGNL(Object-Graph Navigation Language),可以方便地操作对象属性的开源表达式语言: JSTL(JSP Standard T ...
- Linux中vi的使用
首先,如果vi中出现了方向键变成ABCD的情况,需要卸载默认的vim-common,再安装vim. sudo apt-get remove vim-common sudo apt-get instal ...
- CentOS服务器上搭建Gitlab安装步骤、中文汉化详细步骤、日常管理以及异常故障排查
一, 服务器快速搭建gitlab方法 可以参考gitlab中文社区 的教程centos7安装gitlab:https://www.gitlab.cc/downloads/#centos7centos6 ...
- Digest Authentication 摘要认证
“摘要”式认证( Digest authentication)是一个简单的认证机制,最初是为HTTP协议开发的,因而也常叫做HTTP摘要,在RFC2671中描述.其身份验证机制很简单,它采用杂凑式(h ...
- webBrowser 模拟登录
webBrowser1.Document.GetElementById("txtUsername").InnerText = "sdsy";//fill nam ...
- 使用Unity制作的一个望远镜特效,在狙击手游戏中非经常见
仅仅须要编写一个脚本文件,然后就能随意设置放大缩小的速度.以及程度.
- swift - UITextField 的用法
1,文本框的创建,有如下几个样式: public enum UITextBorderStyle : Int { case none 无边框 case line 直线边框 cas ...