Beam Cannon
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 457 Accepted Submission(s): 175
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.
A test case starting with a negative integer terminates the input and this test case should not to be processed.
題意:在一個平面內有N個人,用一個W*H的矩形去圍這些人(邊上的也算), 求最大人數。
思路:以x從小到大排序,y值離散化,投影到y軸上,那麼對於每個人的縱座標,y,y+h就是
每個星星可以影響到的矩形 然後x,x+w+1就是一個進入事件和一個出去事件,其所帶的值互
爲相反數. node[1].val 保存當前的最大值 當所有的矩形都遍歷一遍 取其中的最大值就是ans。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=20005;
struct node
{
int x,y1,y2,val;
void fun(int xx,int yy1,int yy2,int v)
{
x=xx,y1=yy1,y2=yy2;
val=v;
}
}b[maxn];
struct tree
{
int l,r,add,val;
}a[maxn*4];
int n,w,h,cnt,Y[maxn];
bool cmp(node p,node q)
{
return p.x<q.x;
}
void build(int l,int r,int k)
{
a[k].l=l,a[k].r=r;
a[k].add=a[k].val=0;
if(l==r) return ;
int mid=(l+r)/2;
build(l,mid,2*k);
build(mid+1,r,2*k+1);
}
void pushdown(int k)
{
a[2*k].val+=a[k].add;
a[2*k].add+=a[k].add;
a[2*k+1].val+=a[k].add;
a[2*k+1].add+=a[k].add;
a[k].add=0;
}
void insert(int l,int r,int c,int k)
{
if(Y[a[k].l]==l && Y[a[k].r]==r)
{
a[k].val+=c;
a[k].add+=c;
}
else
{
pushdown(k);
int mid=(a[k].l+a[k].r)/2;
if(Y[mid]>=r) insert(l,r,c,2*k);
else if(Y[mid]<l) insert(l,r,c,2*k+1);
else
{
insert(l,Y[mid],c,2*k);
insert(Y[mid+1],r,c,2*k+1);
}
a[k].val=max(a[2*k].val,a[2*k+1].val);
}
}
void input()
{
cnt=0;
int x,y;
scanf("%d %d",&w,&h);
for(int i=0;i<n;i++)
{
scanf("%d %d",&x,&y);
y+=20000;
Y[cnt]=y;
b[cnt++].fun(x,y,y+h,1);
Y[cnt]=y+h;
b[cnt++].fun(x+w+1,y,y+h,-1);
}
sort(Y,Y+cnt);
sort(b,b+cnt,cmp);
cnt=unique(Y,Y+cnt)-Y;
}
void solve()
{
build(0,cnt-1,1);
int ans=0;
for(int i=0;i<2*n;i++)
{
insert(b[i].y1,b[i].y2,b[i].val,1);
ans=max(ans,a[1].val);
}
printf("%d\n",ans);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n<0) break;
input();
solve();
}
return 0;
}