Codevs 2995 楼房

2995 楼房

时间限制: 1 s    空间限制: 256000 KB    题目等级 : 黄金 Gold

题目描述 Description

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

输入描述 Input Description

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

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

输出描述 Output Description

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

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

样例输入 Sample Input

【样例输入】

2
3 0 2
4 1 3

【样例输入2】

5
3 -3 0
2 -1 1
4 2 4
2 3 7
3 6 8

样例输出 Sample Output

【样例输出】

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

数据范围及提示 Data Size & Hint

对于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>
 2 #include<queue>
 3 #include<algorithm>
 4 using namespace std;
 5 #define N 100100
 6 struct ss{
 7     int h,l,r;
 8     bool operator < (const ss &a) const{
 9         if(l==a.l) return h>a.h;
10         return l<a.l;
11     }
12 }e[N<<1];
13 struct node{
14     int h,r;
15     node(ss x){ h=x.h;r=x.r; }
16     bool operator < (const node &a) const{
17         return h<a.h;
18     }
19 };
20 int n,cnt;
21 pair<int,int>ans[N<<2];
22 priority_queue<node>que;
23 ss bfs(ss now,int x){
24     while(!que.empty()){
25         node nown=que.top();que.pop();
26         if(nown.r>now.r){
27             if(nown.h!=now.h){
28                 ans[++cnt]=make_pair(now.r,now.h);
29                 ans[++cnt]=make_pair(now.r,nown.h);
30             }
31             now.r=nown.r;now.h=nown.h;
32         }
33         if(now.r>=x) return now;
34     }
35     ans[++cnt]=make_pair(now.r,now.h);
36     ans[++cnt]=make_pair(now.r,0);
37     now.h=0;now.r=0x7fffffff;
38     return now;
39 }
40 void solve(){
41     ans[++cnt]=make_pair(e[1].l,0);
42     ans[++cnt]=make_pair(e[1].l,e[1].h);
43     ss now=e[1];
44     for(int i=2;i<=n;i++){
45         if(now.h>=e[i].h&&now.r>=e[i].l)que.push(e[i]);
46         if(now.h<e[i].h&&now.r>=e[i].l){
47             que.push(now);
48             ans[++cnt]=make_pair(e[i].l,now.h);
49             now=e[i];
50             ans[++cnt]=make_pair(e[i].l,now.h);
51         }
52         if(now.r<e[i].l){
53             now=bfs(now,e[i].l);
54             if(now.h>=e[i].h&&now.r>=e[i].l)
55               que.push(e[i]);
56             if(now.h<e[i].h&&now.r>=e[i].l){
57                 que.push(now);
58                 ans[++cnt]=make_pair(e[i].l,now.h);
59                 now=e[i];
60                 ans[++cnt]=make_pair(e[i].l,now.h);
61             }
62         }
63     }
64     bfs(now,0x7fffffff);
65 }
66 int main(){
67     scanf("%d",&n);
68     for(int i=1;i<=n;i++)
69       scanf("%d%d%d",&e[i].h,&e[i].l,&e[i].r);
70     sort(e+1,e+1+n);
71     solve();
72     printf("%d
",cnt);
73     for(int i=1;i<=cnt;i++)
74       printf("%d %d
",ans[i].first,ans[i].second);
75     return 0;
76 }
原文地址:https://www.cnblogs.com/suishiguang/p/6368592.html