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; }
相关文章推荐
- HDU 4007 Dave 线段树
- [HDU][4007][Dave]
- HDU 4007 Dave
- HDU 4007 Dave 枚举
- 51nod-1102 . 面积最大的矩形&&hdu-1506
- hdu 4007 Dave
- HDU 1505 City Game (最大子矩形面积)
- hdu 4007 Dave
- HDOJ 4007 Dave【最大覆盖集】
- hdu 2830 逐行扫描dp求最大完全矩形
- hdu(4007)正方形能框住最大的点数
- hdu 4007 Dave
- HDU 4007 Dave【离散化+扫描线】
- City Game - HDU 1505 最大内部矩形
- hdu 4007 Dave (2011年大连ACM网络赛)
- hdu 4007 Dave
- HDU 4007 Dave(11年大连 线段树+离散化+扫描线)
- hdu 4007 Dave(线段树+离散化+扫描线)
- HDU 4007 Dave (基本算法-水题)
- HDU 1506 Largest Rectangle in a Histogram (最大子矩形面积)