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 ...
随机推荐
- python知识点拾遗
内容概要 1.__str__ 2.os.path相关方法 1.__str__ 我们先定义一个Student类,打印一个实例: class Student(object): def __init__(s ...
- Java-使用哈希码比较对象的值
在一些特殊的情况下使用 package com.tj; import java.io.File; public class MyHash { public static void main(Strin ...
- pytorch将cpu训练好的模型参数load到gpu上,或者gpu->cpu上
假设我们只保存了模型的参数(model.state_dict())到文件名为modelparameters.pth, model = Net() 1. cpu -> cpu或者gpu -> ...
- Azure Storage Blob文件重命名
Azure Storage的SDK并没有提供文件重命名的方法,而且从StorageExplorer管理工具里操作修改文件名的时候也有明确提示: 是通过复制当前文件并命名为新文件名再删除旧文件,不保存快 ...
- iOS视频录制,裁剪(输出指定大小)
弄了几天,终于搞懂一点了,博客后面再写,先附上demo,等研究清楚再写 demo地址:http://pan.baidu.com/s/1o6w2CMu
- bzoj3874&2832 [Ahoi2014]宅男计划 模拟退火,三分
[Ahoi2014&Jsoi2014]宅男计划 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 962 Solved: 371[Submit][ ...
- 【JQ同胞遍历】
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Java线程的学习_线程池
系统启动一个新线程需要很高的成本,因为它涉及与操作系统交互.在这种情况下,使用线程池可以很好地提高性能,尤其是当程序中需要创建大量生存期很短暂的线程时. 线程池在系统启动时即创建大量空闲的线程,程序将 ...
- UVa10234 Race
递推,设有i个人排在第一名,剩下的人排在后面,方案有f[i]种,则f[i]=sum(c[n][i]*f[n-i]) 1<=i<=n /*by SilverN*/ #include<a ...
- R语言入门视频笔记--3--列表list
list <- (stud.id = 1234,stud.name="Tom",stud,marks=c(18,3,14,25,19)) #生成一个列表,里面有学生id,学生 ...