码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 5091 Beam Cannon 扫描线

时间:2014-11-04 00:16:53      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   java   


线段树+扫描线:

我们用矩形的中心点来描述这个矩形,然后对于每个敌舰,我们建立一个矩形中心的活动范围,即矩形中心在该范围内活动就可以覆盖到该敌舰.那么我们要求的问题就变成了:任意一个区域(肯定也是矩形的)最多能被矩形覆盖的最大值.

Beam Cannon

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 159    Accepted Submission(s): 59


Problem Description
Recently, 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 it is 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 can be 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.
 

Input
Input 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.
 

Output
Output the maximum number of enemy spaceships the Beam Cannon can destroy in a single shot for each case.
 

Sample Input
2 3 4 0 1 1 0 3 1 1 -1 0 0 1 1 0 -1
 

Sample Output
2 2
 

Source
 




#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;

const int maxn=30100;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

struct SEG
{
  double y1,y2,h;
  int d;
}seg[maxn<<2];

bool cmp(SEG a,SEG b)
{
  if(a.h==b.h) return a.d>b.d;
  return a.h<b.h;
}

int add[maxn<<2],mx[maxn<<2];
int n,sn;
double W,H;

double Y[maxn<<2];
int ny;

void push_up(int rt)
{
  mx[rt]=max(mx[rt<<1],mx[rt<<1|1]);
}

void push_down(int rt)
{
  if(add[rt])
    {
      mx[rt<<1]+=add[rt]; mx[rt<<1|1]+=add[rt];
      add[rt<<1]+=add[rt]; add[rt<<1|1]+=add[rt];
      add[rt]=0;
    }
}

void update(int L,int R,int D,int l,int r,int rt)
{
  if(L<=l&&r<=R)
    {
      add[rt]+=D;
      mx[rt]+=D;
      return ;
    }
  push_down(rt);
  int m=(l+r)/2;
  if(L<=m) update(L,R,D,lson);
  if(R>m) update(L,R,D,rson);
  push_up(rt);
}

int main()
{
  while(scanf("%d",&n)!=EOF&&n>0)
    {
      scanf("%lf%lf",&W,&H);
      sn=0; ny=0;
      for(int i=0;i<n;i++)
        {
          double x,y;
          scanf("%lf%lf",&x,&y);
          seg[sn++]=(SEG){y-H/2,y+H/2,x-W/2,1};
          seg[sn++]=(SEG){y-H/2,y+H/2,x+W/2,-1};
          Y[ny++]=y-H/2; Y[ny++]=y+H/2;
        }

      sort(seg,seg+sn,cmp);
      sort(Y,Y+ny);
      ny=unique(Y,Y+ny)-Y;

      memset(add,0,sizeof(add));
      memset(mx,0,sizeof(mx));

      int ans=0;
      for(int i=0;i<sn;i++)
        {
          int y1=lower_bound(Y,Y+ny,seg[i].y1)-Y+1;
          int y2=lower_bound(Y,Y+ny,seg[i].y2)-Y+1;
          update(y1,y2,seg[i].d,1,ny,1);
          ans=max(ans,mx[1]);
        }
      printf("%d\n",ans);
    }
  return 0;
}


HDOJ 5091 Beam Cannon 扫描线

标签:des   style   blog   http   io   color   ar   os   java   

原文地址:http://blog.csdn.net/ck_boss/article/details/40752505

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!