线段树+扫描线:

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

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;
}

最新文章

  1. MVC 问答
  2. HotApp
  3. Voix.js – 使用声音来控制和操纵你的网站
  4. oracle 存储过程的写法
  5. Java NIO教程 Selector
  6. sql server多数据库查询 远程数据库查询
  7. WPF 自定义控件,在ViewModel里面获取自定义控件的值
  8. java main函数
  9. python15-day1课堂随机
  10. 写的一个Makefile
  11. openStack opts
  12. hdu1102 Constructing Roads (简单最小生成树Prim算法)
  13. 如何使用 VS生成动态库?
  14. Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded
  15. 详细介绍MySQL/MariaDB的锁
  16. metaclass(元类)
  17. How to fix the bug “Expected &quot;required&quot;, &quot;optional&quot;, or &quot;repeated&quot;.”?
  18. CSS 基础 例子 display属性:block、inline和inline-block的区别
  19. ASP入门(二十三)- 数据库插入、更新和删除操作
  20. CentOS 7修改管理员密码

热门文章

  1. centos7 安装nodejs 最新版
  2. undertow的PUT参数获取问题
  3. Redis学习笔记01---配置文件
  4. x86保护模式 任务状态段和控制门
  5. xtu数据结构 C. Ultra-QuickSort
  6. 九度oj 题目1100:最短路径
  7. 【2018.11.23】2018WCTest(7)
  8. Spoj-TRNGL Make Triangle
  9. Vmware虚拟机三种网卡模式详解
  10. Min(BZOJ 1441)