HDU 4007 Dave(双扫描线+技巧暴力)

Dave

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 3645    Accepted Submission(s): 1231


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.

抽象一下题意:给n个点和一个为r的正方形,要求正方形囊括的点数最多;

开始的想法是对于每一个点取以它为顶点的四个为r正方形覆盖的点数,但其实对于一个正方形来说,这个点可能是在边上而不恰好是顶点,因此采用双扫描线的做法;

首先将所有点按横坐标大小排序,然后以p[i].x为第一条竖扫描线,p[i].x+r为第二条竖扫描线,在存下在这个区间内的点的y坐标y_cmp[i];

将y_cmp数组从小到大排序,再以y_cmp[i]为第一条横扫描线,y_cmp[i]+r为第二条横扫描线,如果寻找到满足的坐标,ans++;

最后找出最大的ansMax,记得下一次操作ans前重新赋值~

试过直接不筛点暴力会T,所以必须要用小技巧啊- -~ 




#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
struct Point{
	int x,y;
}p[1010];
int y_cmp[1010];
int cmp(Point a,Point b){
	return a.x==b.x?a.y<b.y:a.x<b.x;
}
int main(){
	int n,r;
	while(~scanf("%d %d",&n,&r)){
		for(int i=1;i<=n;i++){
			scanf("%d %d",&p[i].x,&p[i].y);	
		} 
		sort(p+1,p+n+1,cmp);
		int ans=0,ansMax=0;
		
		for(int i=1;i<=n;i++){
			int xUp=p[i].x+r;
			int k=0;
			for(int j=i;j<=n;j++){
				if(p[j].x<=xUp){
					y_cmp[++k]=p[j].y;
				}
			}
			sort(y_cmp+1,y_cmp+k+1);
			for(int j=1;j<=k;j++){
				int yUp=y_cmp[j]+r;
				ans=0;
				for(int l=j;l<=k;l++){
					if(y_cmp[l]<=yUp){
						ans++;
					}
				}
				ansMax=max(ans,ansMax);
			}
			
			
		}
		

		printf("%d\n",ansMax);
	}
}
/*
1 2
2 1
2 4
2 0
3 2
4 1
4 4
*/

拖了NNNNNNNN久的题目= =~

大概我需要一个暴力的分类了!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值