您的位置:首页 > 其它

hdu5091-Beam Cannon 线段树+扫描线 求矩形内最多能包含多少个点

2017-07-24 16:23 351 查看

Beam Cannon

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1110    Accepted Submission(s): 433Problem DescriptionRecently, the γ galaxies broke out Star Wars. Each planet is warring for resources. In the Star Wars, Planet X is under attack by other planets. Now, a large wave of enemy spaceships is approaching. There is a very large Beam Cannon on the Planet X, and itis very powerful, which can destroy all the spaceships in its attack range in a second. However, it takes a long time to fill the energy of the Beam Cannon after each shot. So, you should make sure each shot can destroy the enemy spaceships as many as possible.To simplify the problem, the Beam Cannon can shot at any area in the space, and the attack area is rectangular. The rectangle parallels to the coordinate axes and cannot rotate. It can only move horizontally or vertically. The enemy spaceship in the space canbe considered as a point projected to the attack plane. If the point is in the rectangular attack area of the Beam Cannon(including border), the spaceship will be destroyed. InputInput contains multiple test cases. Each test case contains three integers N(1<=N<=10000, the number of enemy spaceships), W(1<=W<=40000, the width of the Beam Cannon’s attack area), H(1<=H<=40000, the height of the Beam Cannon’s attack area) in the first line,and then N lines follow. Each line contains two integers x,y (-20000<=x,y<=20000, the coordinates of an enemy spaceship). A test case starting with a negative integer terminates the input and this test case should not to be processed. OutputOutput the maximum number of enemy spaceships the Beam Cannon can destroy in a single shot for each case. Sample Input
2 3 40 11 03 1 1-1 00 11 0-1 Sample Output
22题目大意:给你一个矩形的宽度和长度,然后给你一些点的坐标,询问你,这个矩形能装多少个点。解题思路:我们可以先对这些点进行排序,然后通过扫描线从下到上,依次求得最多点,得到最大的值即可。ac代码
#include<stdio.h>
#include<string.h>#include<algorithm>
using namespace std;
#define maxn 20005
int tmp;
int maxv[maxn<<3],addv[maxn<<3];
struct dian{
int x,y1,y2,w;
}a[maxn];
int cmp(dian n1,dian n2)
{
if (n1.x<n2.x||(n1.x==n2.x&&n1.w>n2.w)) return 1;
return 0;
}
void update(int root,int l,int r,int y1,int y2,int v)
{
if (y1<=l&&y2>=r) addv[root]+=v;
else{
int mid=l+(r-l)/2;
if (y1<=mid) update(root*2,l,mid,y1,y2,v);
if (y2>mid) update(root*2+1,mid+1,r,y1,y2,v);
}
if (r==l) maxv[root]=0;
else maxv[root]=max(maxv[root*2],maxv[root*2+1]);
maxv[root]+=addv[root];
}
void query(int root,int l,int r,int y1,int y2,int add)
{
if (y1<=l&&y2>=r) tmp=max(tmp,maxv[root]+add);
else{
int mid=l+(r-l)/2;
if (y1<=mid) query(root*2,l,mid,y1,y2,add+addv[root]);
if (y2>mid) query(root*2+1,mid+1,r,y1,y2,add+addv[root]);
}
}
int main()
{
int T,n,N,i,w,h,xx,yy,ans;
while (~scanf("%d",&n)&&n!=-1)
{
scanf("%d%d",&w,&h); N=0;
for (i=1;i<=n;i++)
{
scanf("%d%d",&xx,&yy);
a[2*i].x=xx; a[2*i].y1=yy+20001; a[2*i].y2=yy+h+20001; a[2*i].w=1;//对y进行扫描
a[2*i-1].x=xx+w; a[2*i-1].y1=yy+20001; a[2*i-1].y2=yy+h+20001; a[2*i-1].w=-1;
if (h+yy+20001>N) N=h+yy+20001;
}
sort(a+1,a+2*n+1,cmp);//离散化去重
memset(maxv,0,sizeof(maxv));
memset(addv,0,sizeof(addv));
ans=0;
for (i=1;i<=2*n;i++)
{
update(1,1,N,a[i].y1,a[i].y2,a[i].w);
tmp=0;
query(1,1,N,1,N,0);
ans=max(ans,tmp);
}
printf("%d\n",ans);
}
}
题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=5091

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐