HDOJ 5091 Beam Cannon 扫描线
线段树+扫描线:
我们用矩形的中心点来描写叙述这个矩形,然后对于每一个敌舰,我们建立一个矩形中心的活动范围,即矩形中心在该范围内活动就能够覆盖到该敌舰.那么我们要求的问题就变成了:随意一个区域(肯定也是矩形的)最多能被矩形覆盖的最大值.
Beam Cannon
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 159 Accepted Submission(s): 59
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.
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.
2 3 4
0 1
1 0
3 1 1
-1 0
0 1
1 0
-1
2
2
#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 扫描线的更多相关文章
- 线段树+扫描线 HDOJ 5091 Beam Cannon(大炮)
题目链接 题意: 给出若干个点的坐标,用一个W*H的矩形去覆盖,问最多能覆盖几个点. 思路: 这是2014上海全国邀请赛的题目,以前写过,重新学习扫描线.首先把所有点移到第一象限([0, 40000] ...
- hdu 5091 Beam Cannon(扫描线段树)
题目链接:hdu 5091 Beam Cannon 题目大意:给定N个点,如今要有一个W∗H的矩形,问说最多能圈住多少个点. 解题思路:线段的扫描线,如果有点(x,y),那么(x,y)~(x+W,y+ ...
- HDU 5091 Beam Cannon (扫描线思想)
题意:移动一个矩形,使矩形内包含的点尽量多. 思路:把一个点拆成两个事件,一个进(权值为1)一个出(权值为-1),将所有点按照x排序,然后扫描,对于每个x,用一个滑窗计算一下最大值,再移动扫描线.树状 ...
- hdu 5091 Beam Cannon
题目大意: 有n个点(n<=10000),点的坐标绝对值不超过20000,然后问你用一个w*h(1<=w,h<=40000)的矩形,矩形的边平行于坐标轴,最多能盖住多少个点. 刘汝佳 ...
- [POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线
Description Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to re ...
- hdu 5091(线段树+扫描线)
上海邀请赛的一道题目,看比赛时很多队伍水过去了,当时还想了好久却没有发现这题有什么水题的性质,原来是道成题. 最近学习了下线段树扫描线才发现确实是挺水的一道题. hdu5091 #include &l ...
- [线段树]HDOJ5091 Beam Cannon
题意:给n, w, h (1 <= N <= 10000,1 <= W <= 40000,1 <= H <= 40000) $w\times h$是可以射到的范围 ...
- HDU 5091 线段树扫描线
给出N个点.和一个w*h的矩形 给出N个点的坐标,求该矩形最多能够覆盖多少个点 对每一个点point(x.y)右边生成相应的点(x+w,y)值为-1: 纵向建立线段树,从左到右扫描线扫一遍.遇到点则用 ...
- HDU 5091---Beam Cannon(线段树+扫描线)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5091 Problem Description Recently, the γ galaxies bro ...
随机推荐
- ubuntu搭建LAMP全教程
http://jingyan.baidu.com/article/a681b0de36ad683b18434691.html 本经验向你展示如何在ubuntu14.04 环境下搭建apache2 + ...
- CSAPP学习笔记—虚拟内存
CSAPP学习笔记—虚拟内存 符号说明 虚拟内存地址寻址 图9-12展示了MMU如何利用页表来实现这种映射.CPU中的一个控制寄存器,页表基址寄存器(Page Table Base Register, ...
- acm之简单博弈 Nim Bash Wythoff
前些日子我打算开了博弈基础,事后想进行总结下 一句话就是分析必胜或必败,异或为0. 以下内容来自转载: Nim游戏的概述: 还记得这个游戏吗?给出n列珍珠,两人轮流取珍珠,每次在某一列中取至少1颗珍珠 ...
- 【Luogu】P3052摩天大楼里的奶牛(遗传算法乱搞)
一道状压题,但今天闲来无事又用遗传乱搞了一下. 设了一个DNA数组,DNA[i]记录第i个物品放在哪个组里.适应度是n-这个生物的组数+1. 交配选用的是轮盘赌和单亲繁殖——0.3的几率单点变异.(事 ...
- 【字符串】BNUOJ 52781 Book Borders
https://www.bnuoj.com/v3/problem_show.php?pid=52781 [AC] #include<bits/stdc++.h> using namespa ...
- c++ 多线程:线程句柄可以提前关闭,但是线程并没有关闭
很多程序在创建线程都这样写的:ThreadHandle = CreateThread(NULL,0,.....);CloseHandel(ThreadHandle );1,线程和线程句柄(Handle ...
- Vue && Angular 双向绑定检测不到对象属性的添加和删除
由于ES5的限制 Vue && Angular 双向绑定检测不到对象属性的添加和删除 还有数组增加索引.这些改变不会触发change事件.Vue是因为实例化的时候已经把各个属性都s ...
- uva 10710 快速幂取模
//题目大意:输入一个n值问洗牌n-1次后是不是会变成初始状态(Jimmy-number),从案例可看出牌1的位置变化为2^i%n,所以最终判断2^(n-1)=1(mod n)是否成立#include ...
- poj 3311 状压dp 最短路
C - Hie with the Pie Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- 计算机windows7连接打印机
计算机连接打印机 (1)查看打印机的名字. 示例,打印机名为 HP LaserJet M1522 .... (2)打开windows开始菜单,点击[设备和打印机],然后查看打印机和传真那个区域是否有打 ...