楼房(洛谷 1382)

题目描述

地平线(x轴)上有n个矩(lou)形(fang),用三个整数h[i],l[i],r[i]来表示第i个矩形:矩形左下角为(l[i],0),右上角为(r[i],h[i])。地平线高度为0。在轮廓线长度最小的前提下,从左到右输出轮廓线。

下图为样例2。

输入输出格式

输入格式:

 

第一行一个整数n,表示矩形个数

以下n行,每行3个整数h[i],l[i],r[i]表示第i个矩形。

 

输出格式:

 

第一行一个整数m,表示节点个数

以下m行,每行一个坐标表示轮廓线上的节点。从左到右遍历轮廓线并顺序输出节点。第一个和最后一个节点的y坐标必然为0。

 

输入输出样例

输入样例#1:
【样例输入1】
2
3 0 2
4 1 3

【样例输入2】
5
3 -3 0
2 -1 1
4 2 4
2 3 7
3 6 8
输出样例#1:
【样例输出1】
6
0 0
0 3
1 3
1 4
3 4
3 0

【样例输出2】
14
-3 0
-3 3
0 3
0 2
1 2
1 0
2 0
2 4
4 4
4 2
6 2
6 3
8 3
8 0

说明

【数据范围】

对于30%的数据,n<=100

对于另外30%的数据,n<=100000,1<=h[i],l[i],r[i]<=1000

对于100%的数据,1<=n<=100000,1<=h[i]<=10^9,-10^9<=l[i]<r[i]<=10^9

做法1:

/*
  扫描线+堆
  我们把每一个楼房的两边(墙壁)的信息记录下来,然后按照墙壁的横坐标排序(具体方法见代码),当扫到楼房左边时,如果它是最高的,就把答案记录下来,否则入堆,扫到楼房右边时,如果它与当前最高的一样高并且最高的只有一条线,记录答案,删除对它对应的左边墙壁,否则删除左边墙壁信息,不进行其他操作。 
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<set>
#define M 200010
using namespace std;
int n,tot,num,xx[M*5],hh[M*5];
struct node
{
    int x,h,k;
};node a[M*2];
multiset<int> s;
int read()
{
    char c=getchar();int num=0,flag=1;
    while(c<'0'||c>'9'){if(c=='-')flag=-1;c=getchar();}
    while(c>='0'&&c<='9'){num=num*10+c-'0';c=getchar();}
    return num*flag;
}
bool cmp(const node&s1,const node&s2)//关键的排序 
{
    if(s1.x!=s2.x)return s1.x<s2.x;//从左到右 
    if(s1.k!=s2.k)return s1.k<s2.k;//这样保证出现此情况时,不会输出重合的边 
    if(s1.k==1)return s1.h>s2.h;//高的覆盖低的 
    if(s1.k==2)return s1.h<s2.h;//同理 
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)
    {
        int h=read(),l=read(),r=read();
        a[++tot].h=h;a[tot].x=l;a[tot].k=1;
        a[++tot].h=h;a[tot].x=r;a[tot].k=2;
    }
    sort(a+1,a+tot+1,cmp);
    s.insert(0);
    for(int i=1;i<=tot;i++)
      if(a[i].k==1)
      {
          int maxh=*s.rbegin();
        if(a[i].h<=maxh)s.insert(a[i].h);
        else
        {
            ++num;xx[num]=a[i].x;hh[num]=maxh;
            ++num;xx[num]=a[i].x;hh[num]=a[i].h;
            s.insert(a[i].h);
        }
      }
      else
      {
          int maxh=*s.rbegin();
          if(a[i].h==maxh&&s.count(maxh)==1)
          {
              s.erase(maxh);
              ++num;xx[num]=a[i].x;hh[num]=a[i].h;
              ++num;xx[num]=a[i].x;hh[num]=*s.rbegin();
        }
        else s.erase(s.find(a[i].h));
      }
    printf("%d\n",num);
    for(int i=1;i<=num;i++)
      printf("%d %d\n",xx[i],hh[i]);
    return 0;
}
View Code

 做法2:

/*
  离散化+线段树+模拟
  由于数据量太大,我们先把所有的墙壁离散化,用线段树维护每个离散化后的横坐标的最高点,
  然后模拟求出答案。 
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#define lson l,m,now*2
#define rson m+1,r,now*2+1
#define M 200010
using namespace std;
int mx[M*4],tag[M*4],a[M*2],b[M*2],ans1[M*5],ans2[M*5],n,cnt=1;
struct node
{
    int x,y,h;
};node q[M];
int read()
{
    char c=getchar();int num=0,flag=1;
    while(c<'0'||c>'9'){if(c=='-')flag=-1;c=getchar();}
    while(c>='0'&&c<='9'){num=num*10+c-'0';c=getchar();}
    return num*flag;
}
void push_up(int now)
{
    mx[now]=max(mx[now*2],mx[now*2+1]);
}
void push_down(int now)
{
    if(!tag[now])return;
    tag[now*2]=max(tag[now],tag[now*2]);
    tag[now*2+1]=max(tag[now],tag[now*2+1]);
    mx[now*2]=max(tag[now],mx[now*2]);
    mx[now*2+1]=max(tag[now],mx[now*2+1]);
    tag[now]=0;
}
void change(int x,int y,int v,int l,int r,int now)
{
    if(l>=x&&r<=y)
    {
        mx[now]=max(mx[now],v);
        tag[now]=max(tag[now],v);
        return;
    }
    push_down(now);
    int m=(l+r)/2;
    if(m>=x)change(x,y,v,lson);
    if(y>m)change(x,y,v,rson);
    push_up(now);
}
int query(int x,int l,int r,int now)
{
    if(l==r)return mx[now];
    int m=(l+r)/2;
    push_down(now);
    if(x<=m)return query(x,lson);
    else return query(x,rson);
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d",&q[i].h,&q[i].x,&q[i].y);
        a[i*2-1]=q[i].x;a[2*i]=q[i].y;
    }
    sort(a+1,a+2*n+1);b[1]=a[1];
    for(int i=2;i<=2*n;i++)
      if(a[i]!=a[i-1])b[++cnt]=a[i];
    for(int i=1;i<=n;i++)
    {
        int x=lower_bound(b+1,b+cnt+1,q[i].x)-b;
        int y=lower_bound(b+1,b+cnt+1,q[i].y)-b;
        change(x,y-1,q[i].h,1,cnt,1);
    }
    int tot=0;
    for(int i=1;i<=cnt;i++)
    {
        ans1[i]=b[i];ans2[i]=query(i,1,cnt,1);
        if(ans2[i]!=ans2[i-1])tot++;
    }
    printf("%d\n",tot*2);
    for(int i=1;i<=cnt;i++)
      if(ans2[i]!=ans2[i-1])
      {
          printf("%d %d\n",ans1[i],ans2[i-1]);
          printf("%d %d\n",ans1[i],ans2[i]);
      }
    return 0;
}
View Code

 

posted @ 2016-09-03 16:32  karles~  阅读(548)  评论(2编辑  收藏  举报