hdu 4007 Dave(线性探查+枚举)

Problem Description

Recently, Dave is boring, so he often walks around. He finds that some places are too crowded, for example, the ground. He couldn‘t help to think of the disasters happening recently. Crowded place is not safe. He knows there are N (1<=N<=1000) people on the ground. Now he wants to know how many people will be in a square with the length of R (1<=R<=1000000000). (Including boundary).

Input

The input contains several cases. For each case there are two positive integers N and R, and then N lines follow. Each gives the (x, y) (1<=x, y<=1000000000) coordinates of people. 

Output

Output the largest number of people in a square with the length of R.

Sample Input

3 2
1 1
2 2
3 3

Sample Output

3

Hint

If two people stand in one place, they are embracing.

Source

The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest

题目意思:

给你N个点和一个边长为R的正方形,为你用这个正方形最多可以覆盖几个点(在正方形边界上的点也算)。

 

注意:正方形的边一定平行于坐标轴。

 

思路:

对N个点的y坐标进行排序,然后O(N)枚举正方形的下边界。取出N个点中y坐标在上下边界范围内的点。然后O(N)复杂度求出长度为R的边最多可以覆盖的点。所以整体枚举的复杂度为O(N^2)。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cmath>
 7 #include<stdlib.h>
 8 #include<map>
 9 using namespace std;
10 #define inf 1<<30
11 #define N 1006
12 int n,r;
13 struct Node{
14     int x,y;
15 }p[N];
16 int yy[N];
17 int xx[N];
18 int main()
19 {
20     while(scanf("%d%d",&n,&r)==2){
21         for(int i=0;i<n;i++){
22             scanf("%d%d",&p[i].x,&p[i].y);
23             yy[i]=p[i].y;
24         }
25         sort(yy,yy+n);
26
27         int ans=0;
28         for(int i=0;i<n;i++){
29             //int y=yy[i];
30             int xcnt=0;
31             for(int j=0;j<n;j++){
32                 if(p[j].y<=yy[i]+r && p[j].y>=yy[i]){
33                     xx[xcnt++]=p[j].x;
34                 }
35             }
36             sort(xx,xx+xcnt);
37
38             int e=0;
39             //xx[xcnt++]=inf;
40             for(int j=0;j<xcnt;j++){
41                 while(xx[e]-xx[j]<=r && e<xcnt) e++;
42                 ans=max(ans,e-j);
43             }
44
45         }
46         printf("%d\n",ans);
47
48
49
50     }
51     return 0;
52 }

时间: 2024-10-28 21:28:50

hdu 4007 Dave(线性探查+枚举)的相关文章

HDU 4007 Dave (基本算法-水题)

Dave Problem Description Recently, Dave is boring, so he often walks around. He finds that some places are too crowded, for example, the ground. He couldn't help to think of the disasters happening recently. Crowded place is not safe. He knows there

HDU 4007 Dave(离散化)

Dave Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 4095    Accepted Submission(s): 1390 Problem Description Recently, Dave is boring, so he often walks around. He finds that some places are to

HDU 3709 Balanced Number 枚举+数位DP

枚举支点之后数位DP,注意姿势 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list&g

HDU 4587 TWO NODES 枚举+割点

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587 TWO NODES Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1448    Accepted Submission(s): 441 Problem Description Suppose that G is an undir

There is a war (hdu 2435 最小割+枚举)

There is a war Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 970    Accepted Submission(s): 277 Problem Description There is a sea. There are N islands in the sea. There are some directional

!hdu 4091--贪心、枚举--(思维)

题意:有一个容量为n的箱子,有两种珠宝,占的体积和价值分别是s1,v1,s2,v2,求能装下的最大的价值总量. 分析:刚开始以为是背包问题,但其实没那么简单:后来分情况讨论还是不行:后来又暴力枚举,当然是超时了.正确做法是有一部分是用背包的贪心,另一部分用枚举!现在就是看哪一部分用贪心. 看别人的题解是:L = LCM(s1,s2),可是知道大于L的部分就用价值比高的,所以n%L的部分枚举,其余部分贪心,不过这样可能得到的还不是最优解,所以n%L+L的部分枚举,这样就可以了,具体原因我还没想明白

hdu 3189(网络流+二分枚举)

Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6422   Accepted: 2202 Description Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns which, of course, have limited capacity. So

hdu 4517(递推枚举统计)

小小明系列故事——游戏的烦恼 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1330    Accepted Submission(s): 426 Problem Description 小小明最近在玩一款游戏,它由n*m大小的矩阵构成,矩阵上会随机产生一些黑色的点,这些点它们可能会连在一起也可能会分开,这些点的个数没有限制,但 是每个

HDU 5358 First One(枚举)

这道题如果按照表达式一个个来算肯定超时,下午时候想了一个O(nlogn*logn)的算法,但是t了,因为这道题卡的非常紧几百个样例,必须nlogn的算法才可以ac 回到这道题,考虑log(sum(i,j))+1的特点,可以发现它的值域范围非常小,在1-34之间,那么我们可以考虑枚举log(sum(i,j)+1的值,记为k,然后统计(i+j)的和即可. 对于每一个k,找到所有满足2^(k-1)<=sum(i,j)<=2^k-1的(i+j), 那么我们考虑每个前缀i,找到这个前缀满足2^(k-1)