杭电 4007 亚洲区域赛大连赛区

        这道题是一个纠结啊,比赛时就不说了,,,看都没有看的,后来挺mdd学长讲了一下思路,又是离散又是dp的,当时听的头就大了,一直写不出来代码,后来看网上说黑书上有原题,,,二话不说,翻黑书,,接下来就是真正的悲剧,黑书上的思路挺经典的,代码也是挺简洁的,就是我一直理解不了。。想了几天了,今天中午终于想明白了,,,唉,,这叫一个过程漫长啊,,失败,,还是太弱小了。。。。

代码中的难点:为什么要将一个不存在的点赋值为-1,,我想这个问题想了好几天。原来和里面存的是如果包含那个点,最多能圈几个点,因为那个点本来就是不存在的,所以要将其赋值为-1.。。。。。

题目:

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
ac代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
struct point
{
  int x;
  int y;
}aa[1005],bb[10000];
bool cmp1(point a,point b)
{
  if(a.x==b.x)
	  return a.y<b.y;
  return a.x<b.x;
}
bool cmp2(point a,point b)
{
  return a.x<b.x;
}
int main()
{
  int n,r;
  while(scanf("%d%d",&n,&r)!=EOF)
  {
    for(int i=0;i<n;++i)
		scanf("%d%d",&aa[i].x,&aa[i].y);
	sort(aa,aa+n,cmp1);
	int top,sum,mmax=0,mmmax=0;
	for(int i=0;i<n;++i)
	{
	  top=0;sum=0;
	  int str=aa[i].x,end=aa[i].x+r;
	  for(int j=0;j<n;++j)
	  {
	    if(str<=aa[j].x&&aa[j].x<=end)
		{
		  bb[top].x=aa[j].y;
		  bb[top].y=1;
		  top++;
		  bb[top].x=aa[j].y+r+1;
		  bb[top].y=-1;
		  top++;
		}
	  }
	  //cout<<top<<endl;
	  sort(bb,bb+top,cmp2);
	   mmax=0;
	  for(int i=0;i<top;++i)
	 {
	   sum+=bb[i].y;
	   //cout<<sum<<endl;
	   mmax=sum>mmax?sum:mmax;
	  
	 }
	  mmmax=mmmax>mmax?mmmax:mmax;
	}
	
	printf("%d\n",mmmax);
  }
  return 0;
}


你可能感兴趣的:(input,each,output)