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

本文解决了一个有趣的问题:计算边长为R的正方形内最多能容纳多少人。通过输入人群坐标,采用排序和扫描线算法,实现了高效求解。文章记录了从困惑到理解的过程,并分享了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        这道题是一个纠结啊,比赛时就不说了,,,看都没有看的,后来挺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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值