线段树+扫描线:

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

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 扫描线的更多相关文章

  1. 线段树+扫描线 HDOJ 5091 Beam Cannon(大炮)

    题目链接 题意: 给出若干个点的坐标,用一个W*H的矩形去覆盖,问最多能覆盖几个点. 思路: 这是2014上海全国邀请赛的题目,以前写过,重新学习扫描线.首先把所有点移到第一象限([0, 40000] ...

  2. hdu 5091 Beam Cannon(扫描线段树)

    题目链接:hdu 5091 Beam Cannon 题目大意:给定N个点,如今要有一个W∗H的矩形,问说最多能圈住多少个点. 解题思路:线段的扫描线,如果有点(x,y),那么(x,y)~(x+W,y+ ...

  3. HDU 5091 Beam Cannon (扫描线思想)

    题意:移动一个矩形,使矩形内包含的点尽量多. 思路:把一个点拆成两个事件,一个进(权值为1)一个出(权值为-1),将所有点按照x排序,然后扫描,对于每个x,用一个滑窗计算一下最大值,再移动扫描线.树状 ...

  4. hdu 5091 Beam Cannon

    题目大意: 有n个点(n<=10000),点的坐标绝对值不超过20000,然后问你用一个w*h(1<=w,h<=40000)的矩形,矩形的边平行于坐标轴,最多能盖住多少个点. 刘汝佳 ...

  5. [POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线

    Description  Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to re ...

  6. hdu 5091(线段树+扫描线)

    上海邀请赛的一道题目,看比赛时很多队伍水过去了,当时还想了好久却没有发现这题有什么水题的性质,原来是道成题. 最近学习了下线段树扫描线才发现确实是挺水的一道题. hdu5091 #include &l ...

  7. [线段树]HDOJ5091 Beam Cannon

    题意:给n, w, h  (1 <= N <= 10000,1 <= W <= 40000,1 <= H <= 40000) $w\times h$是可以射到的范围 ...

  8. HDU 5091 线段树扫描线

    给出N个点.和一个w*h的矩形 给出N个点的坐标,求该矩形最多能够覆盖多少个点 对每一个点point(x.y)右边生成相应的点(x+w,y)值为-1: 纵向建立线段树,从左到右扫描线扫一遍.遇到点则用 ...

  9. HDU 5091---Beam Cannon(线段树+扫描线)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5091 Problem Description Recently, the γ galaxies bro ...

随机推荐

  1. python类可以截获Python运算符

    类可以截获Python运算符 现在,让我们来看类和模块的第三个主要差别: 运算符重载.简而言之,运算符重载就是让用类写成的对象,可截获并响应用在内置类型上的运算:加法.切片.打印和点号运算等.这只是自 ...

  2. HashTable的构造函数有哪些

    HashTable:在并发的环境下,使用synchronized将整张表锁住: HashTable构造函数有: public Hashtable(int initialCapacity, float ...

  3. @Java Web 程序员,我们一起给程序开个后门吧:让你在保留现场,服务不重启的情况下,执行我们的调试代码

    一.前言 这篇算是类加载器的实战第五篇,前面几篇在这里,后续会持续写这方面的一些东西. 实战分析Tomcat的类加载器结构(使用Eclipse MAT验证) 还是Tomcat,关于类加载器的趣味实验 ...

  4. 思维定势--AtCoder Regular Contest 092 D - Two Sequences

    $n \leq 100000$的俩序列,数字范围$2^{28}$,问所有$a_i+b_j$的$n^2$个数字的异或和. 这种东西肯定是按位考虑嘛,从低位开始然后补上进位.比如说第一位俩串分别有$c$个 ...

  5. [MFC] TabControl选项卡的使用

    MFC中,因项目需要使用TabControl ,使用过程中发现,MFC中的TabControl与C#的TabControl不同,不能通过属性来创建选项页,只能代码生成绑定. 以下为具体的实现方法步骤: ...

  6. 强大的stringstream

    [本文来自]http://www.builder.com.cn/2003/0304/83250.shtmlhttp://www.cppblog.com/alantop/archive/2007/07/ ...

  7. Python入门--1--基本中的基本

    一. 1.这是一个面向对面的编程,一种解释性语言. 2.缩进是python的灵魂,使代码变得非常简洁,正确使用冒号“:”,IDLE的       下一行会自动缩进 3.if语句中 python拒绝接受 ...

  8. Android开发之(1)AnimationListener

    1,就像Button控件有监听器一样,动画效果也有监听器,只需要实现AnimationListener就可以实现对动画效果的监听,只需要实现AnimationListener就可以实现对动画效果的监听 ...

  9. 2014湘潭全国邀请赛I题 Intervals /POJ 3680 / 在限制次数下取有权区间使权最大/小问题(费用流)

    先说POJ3680:给n个有权(权<10w)开区间(n<200),(区间最多数到10w)保证数轴上所有数最多被覆盖k次的情况下要求总权最大,输出最大权. 思路:       限制的处理:s ...

  10. 激活win10系统的方法(亲测)

    WIN+X 按A (或者点击左下角有个windows小图标“鼠标右键”选择选择“命令提示符号(管理员)”) 输入下面命令,回车(一行按一个回车键)slmgr.vbs /upkslmgr /ipk W2 ...