题目描述

约翰打算建一个围栏来圈养他的奶牛.作为最挑剔的兽类,奶牛们要求这个围栏必须是正方 形的,而且围栏里至少要有C< 500)个草场,来供应她们的午餐.

约翰的土地上共有C<=N<=500)个草场,每个草场在一块1x1的方格内,而且这个方格的 坐标不会超过10000.有时候,会有多个草场在同一个方格内,那他们的坐标就会相同.

告诉约翰,最小的围栏的边长是多少?

输入输出格式

输入格式:

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.

输出格式:

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.

输入输出样例

输入样例#1:

3 4

1 2

2 1

4 1

5 2

输出样例#1:

4

说明

Explanation of the sample:

|* *

| * *

+——Below is one 4x4 solution (C’s show most of the corral’s area); many others exist.

|CCCC

|CCCC

|CCC

|C*C*

+——

【题解】

咦,我竟然是0ms跑过的最优解。。。那就发个题解总结一下。

二维双指针法的完美结合

双指针法就是令l=1,从1到n枚举右指针r,然后始终保证[l,r]的区间是满足题目要求的区间,不满足就使l++,并每次用[l,r]更新答案(感觉就是简化的单调队列)

如果坐标都是一维的,我们只用双指针法就能搞定,但是由于是二维的,所以我们要用两重双指针法(四指针法。。。)

先二分答案,分别用双指针法维护纵坐标和横坐标,具体还是看代码吧~

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#define ll long long
#define re register
#define il inline
#define fp(i,a,b) for(re int i=a;i<=b;i++)
#define fq(i,a,b) for(re int i=a;i>=b;i--)
using namespace std;
int n,m,nx,ny;
struct field
{
int x,y;
}p[505];
int rx[505],ry[505],s[505];
il bool cmp1(field a,field b)
{
return a.x<b.x;
}
il bool cmp2(field a,field b)
{
return a.y<b.y;
}
il 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 1;
}
}
return 0;
}
il int gi()
{
re int x=0;
re short int t=1;
re char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if(ch=='-') t=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*t;
}
int main()
{
m=gi();n=gi();
rx[0]=ry[0]=-1;
fp(i,1,n) p[i].x=gi(),p[i].y=gi();
sort(p+1,p+1+n,cmp2);
fp(i,1,n)
{
if(p[i].x>rx[nx]) ry[++ny]=p[i].y;
p[i].y=ny;
}
sort(p+1,p+1+n,cmp1);
fp(i,1,n)
{
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;
}

最新文章

  1. 浅谈ListView滑动隐藏显示ToolBar
  2. 如何利用cookie来保存用户登录账号
  3. js中获得指定范围的n个不重复的随机数
  4. sqlserver sum 和count在关于进行统计时的区别
  5. ASP.NET MVC路由配置(转载自http://www.cnblogs.com/zeusro/p/RouteConfig.html )
  6. mysql 更改自动增长字段值的重新设定
  7. MongoDB学习笔记-数据格式及数据类型
  8. EF Code First学习笔记
  9. NYOJ-36 最长公共子序列 AC 分类: NYOJ 2014-01-03 20:54 155人阅读 评论(0) 收藏
  10. HTML5画布(图像)
  11. 流媒体开发之--HLS--M3U8解析(2): HLS草案
  12. Java EE (12) -- 系统质量的分类
  13. RH253读书笔记(7)-Lab 7 Electronic Mail
  14. 解释 : translate 功能,过程
  15. Ubuntu 下 Galera cluster for MySQL 集群安装
  16. python模块安装查看、包制作
  17. .net缓存的应用研究(读篇)
  18. FreeSWITCH部署与功能配置
  19. CODEVS.3990.中国余数定理2(CRT)
  20. RabbitVCS - Ubuntu VCS Graphical Client

热门文章

  1. 【原】Python学习
  2. scala学习(3)-----wordcount【sparksession】
  3. C/C++野指针
  4. HDU_5783_DivideTheSequence
  5. Ztree加载完成默认选中根节点右侧生成表格
  6. Python使用Flask框架,结合Highchart,搭配数据功能模块,加载 HTML 表格数据
  7. git帮助网址
  8. SpringBoot yaml的配置及使用
  9. Maven_运行时环境
  10. Convolutions in TensorFlow