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

扫描二维码关注公众号,回复: 3091775 查看本文章
 

3 2 1 1 2 2 3 3

 

Sample Output

 
3

Hint

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

题意:给n个点的坐标,给一个边长为r的正方形,求正方形内最多有多少点。

对y坐标的点排序,然后O(n)对N个坐标枚举这个正方形的上下界,然后取出y坐标在正方形内的点,并记录y坐标对应的x坐标。最

后再对这些x坐标进行枚举,看是否在这个正方形中。

但是不知道为什么我想同时枚举两个点却不行,求大佬指点;

(1)自己想的;

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int M=2e3+10;
struct node{
	ll x,y;
}p[M];
int t[M];
bool cmp(node a,node b){
	if(a.x!=b.x) return a.x<b.x;
	else return a.y<b.y;
}
int main()
{
	int n,r;
	while(scanf("%d%d",&n,&r)!=EOF){
		memset(p,0,sizeof(p));
		memset(t,0,sizeof(t));
		for(int i=0;i<n;i++){
			scanf("%lld%lld",&p[i].x ,&p[i].y);
		}
		sort(p,p+n,cmp);
		ll ans=0;
		for(int i=0;i<n;i++){
			ll st1=p[i].x;
			ll en1=p[i].x+r;
			int k=0;
			for(int j=0;j<n;j++){
				if(p[j].x>=st1&&p[j].x<=en1) {
					t[k++]=p[j].y;
				}	
			}
			sort(t,t+k);
			ll sum=0,cnt=0;
			for(int j=0;j<k;j++){
				cnt=0;
				ll st2=t[j];
				ll en2=t[j]+r;//每次都从j开始 
				while(cnt+j<k&&t[j+cnt]>=st2&&t[j+cnt]<=en2) cnt++;
				sum=max(sum,cnt);
			}
			ans=max(ans,sum);
		}
		printf("%lld\n",ans);
	}
	return 0;
}

(2)看的别人的; 

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int M=2005;
struct node{
	ll x,y;
}p[M];
int t[M];
bool cmp(node a,node b){
	if(a.x!=b.x)  
		return a.x<b.x;
	else  return a.y<b.y;
} 
int main()
{
	int n,r;
	while(scanf("%d%d",&n,&r)!=EOF){
		memset(p,0,sizeof(p));
		memset(t,0,sizeof(t));
		for(int i=0;i<n;i++){
			scanf("%lld%lld",&p[i].x ,&p[i].y);
		}
		sort(p,p+n,cmp);
		int ans=0;
		for(int i=0;i<n;i++){
			ll st1=p[i].x;
			ll en1=p[i].x+r;
			int k=0;
			for(int j=0;j<n;j++){
				if(st1<=p[j].x&&p[j].x<=en1){
					t[k++]=p[j].y;
				}
			}
			sort(t,t+k);
			int cnt=0,sum=0;
			for(int j=0;j<k;j++){
				ll en2=t[j]+r;//边界
				while(cnt<k&&t[cnt]<=en2) cnt++;
				sum=max(sum,cnt-j);//?? 
			}
			ans=max(ans,sum);
		}
		printf("%d\n",ans);
	}
	return 0;
}

wrong  answer 的代码: 

这个是每一次都固定一个正方形,有的不是从一个端点固定一个正方形,如下:

如果输入:

6 4
1 2
1 3
2 3
3 1
4 2
3 4
输出:5

但是正确结果是:6;

                  

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int M=2005;
struct node{
	ll x,y;
}p[M];
bool cmp(node a,node b){
	if(a.x!=b.x)  
		return a.x<b.x;
	else  return a.y<b.y;
} 
int main()
{
	int n,r;
	while(scanf("%d%d",&n,&r)!=EOF){
		memset(p,0,sizeof(p));
		for(int i=0;i<n;i++){
			scanf("%lld%lld",&p[i].x ,&p[i].y);
		}
		sort(p,p+n,cmp);
		int ans=0;
		for(int i=0;i<n;i++){
			ll st1=p[i].x, en1=p[i].x+r;
			ll st2=p[i].y, en2=p[i].y+r;
			int sum=0;
			for(int j=0;j<n;j++){
				if((st1<=p[j].x&&p[j].x<=en1) && (st2<=p[j].y&&p[j].y<=en2)){
					sum++;
				}
			}
			//cout<<"sum: "<<sum<<endl;
			ans=max(ans,sum);
		}
		printf("%d\n",ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41555192/article/details/82386818
今日推荐