您的位置:首页 > 其它

hdu 4007 Dave 求矩形圈点最大值

2014-07-17 08:37 337 查看
对于每个点,扩成一个矩形,变成求最大重叠层数;

注意边界的处理,这里是横纵均加一;

可能在边界上会产生一些问题,在排序的时候修正;
#include<algorithm>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<cmath>
#include<math.h>
using namespace std;
#define inf 0x3f3f3f3f
const int dir[4][2]={0,1,0,-1,1,0,-1,0};
#define maxn 2005

struct Tree
{
int l,r;//左右端点
int c;//覆盖情况
int cnt,lf,rf;//cnt为层数,lf为实际左坐标,rf为实际右坐标
}tree[maxn*4];

struct Line
{
int x,y1,y2;
int f;//直线的标记
}line[maxn];

bool cmp(Line a,Line b)//sort排序的函数
{
if(a.x!=b.x)
return a.x < b.x;
else
return a.f < b.f;
}

int y[maxn];//记录y坐标

void build(int l,int r,int root)
{
tree[root].l=l;tree[root].r=r;
tree[root].c=tree[root].cnt=0;
tree[root].lf=y[l];
tree[root].rf=y[r];
if(l+1==r)return;
int mid=(l+r)>>1;
build(l,mid,root<<1);
build(mid,r,root<<1|1);
}

void calen(int root)
{
if(tree[root].l+1==tree[root].r) tree[root].cnt=tree[root].c;
else tree[root].cnt=max(tree[root<<1].cnt,tree[root<<1|1].cnt)+tree[root].c;
}

void update(int root,struct Line e)
{
if(e.y1==tree[root].lf&&e.y2==tree[root].rf)
{
tree[root].c+=e.f;
calen(root);
return;
}
if(e.y2<=tree[root<<1].rf)update(root<<1,e);
else if(e.y1>=tree[root<<1|1].lf)update(root<<1|1,e);
else
{
Line tmp=e;
tmp.y2=tree[root<<1].rf;
update(root<<1,tmp);
tmp=e;
tmp.y1=tree[root<<1|1].lf;
update(root<<1|1,tmp);
}
calen(root);
}

int main()
{
int i,j;
int n,r,k;
int x1,y1,x2,y2;
while(scanf("%d%d",&n,&r)!=EOF)
{
k=1;
for(i=1;i<=n;i++)
{
scanf("%d%d",&x1,&y1);
x2=x1+r+1;
y2=y1+r+1;
line[k].x=x1;
line[k].y1=y1;
line[k].y2=y2;
line[k].f=1;
y[k]=y1;
k++;
line[k].x=x2;
line[k].y1=y1;
line[k].y2=y2;
line[k].f=-1;
y[k]=y2;
k++;
}
k--;
sort(line+1,line+k+1,cmp);
sort(y+1,y+k+1);
int mm;
mm=unique(y+1,y+1+k)-y-1;
build(1,mm,1);
int ans=0;
for(i=1;i<=k;i++)
{
update(1,line[i]);
if(tree[1].cnt>ans)
ans=tree[1].cnt;
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: