hdu 4007 Dave
2016-07-04 07:48
211 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4007
题意:给n个点的坐标,给一个边长为r的正方形,求正方形内最多有多少点。
这题给的正方形实际上是平行于x轴的,题意没表达清楚。所以用暴力就可以过,排序,平行x轴,排序,然后再平移y。
#include<bits/stdc++.h>
#define N 1100
using namespace std;
struct node
{
int x,y;
}p
;
int n,r,t
;
bool cmp(node a,node b)
{
return a.x<b.x;
}
int main()
{
while(~scanf("%d%d",&n,&r))
{
for(int i=0;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y);
sort(p,p+n,cmp);
int ans=0;
for(int i=0;i<n;i++)
{
int x=p[i].x+r;
int m=0;
for(int j=i;p[j].x<=x&&j<n;j++) t[m++]=p[j].y;
sort(t,t+m);
int cnt=0,sum=0;
for(int j=0;j<m;j++)
{
int y=t[j]+r;
while(cnt<m&&t[cnt]<=y) cnt++;
sum=max(sum,cnt-j);
}
ans=max(ans,sum);
}
cout<<ans<<endl;
}
}
题意:给n个点的坐标,给一个边长为r的正方形,求正方形内最多有多少点。
这题给的正方形实际上是平行于x轴的,题意没表达清楚。所以用暴力就可以过,排序,平行x轴,排序,然后再平移y。
#include<bits/stdc++.h>
#define N 1100
using namespace std;
struct node
{
int x,y;
}p
;
int n,r,t
;
bool cmp(node a,node b)
{
return a.x<b.x;
}
int main()
{
while(~scanf("%d%d",&n,&r))
{
for(int i=0;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y);
sort(p,p+n,cmp);
int ans=0;
for(int i=0;i<n;i++)
{
int x=p[i].x+r;
int m=0;
for(int j=i;p[j].x<=x&&j<n;j++) t[m++]=p[j].y;
sort(t,t+m);
int cnt=0,sum=0;
for(int j=0;j<m;j++)
{
int y=t[j]+r;
while(cnt<m&&t[cnt]<=y) cnt++;
sum=max(sum,cnt-j);
}
ans=max(ans,sum);
}
cout<<ans<<endl;
}
}
相关文章推荐
- java入门--标识符、关键字和注释
- leetcode201. Bitwise AND of Numbers Range
- MongoDB 性能测试
- 2016 计蒜之道 复赛 一些简单题的Solution
- mapreduce编程模型你知道多少?
- Js和html相结合的方式
- Android线程,线程池使用及原理博文参考
- leetcode: Palindrome Partitioning II
- J2EE应用结构
- Mac上的抓包工具Charles
- Linux下读取默认MAC地址
- 使用 systemd timer 备份数据库
- 上周热点回顾(6.27-7.3)
- 3Sum & 4Sum
- Fiddler抓取Android真机上的HTTPS包
- .NET Core系列 : 1、.NET Core 环境搭建和命令行CLI入门
- Two Sum I & II
- 从背单词到写代码
- 在工作中如何提高开发工作效率
- 当我们谈论跳槽时在谈论什么