uva 105 The Skyline Problem

原题:
With the advent of high speed graphics workstations, CAD (computer-aided design) and other areas(CAM, VLSI design) have made increasingly effective use of computers. One of the problems with drawing images is the elimination of hidden lines — lines obscured by other parts of a drawing.You are to design a program to assist an architect in drawing the skyline of a city given the locations of the buildings in the city. To make the problem tractable, all buildings are rectangular in shape and they share a common bottom (the city they are built in is very flat). The city is also viewed as twodimensional. A building is specified by an ordered triple (L i ,H i ,R i ) where L i and R i are left and right coordinates, respectively, of building i (Li < Ri) and H i is the height of the building. In the diagram below buildings are shown on the left with triples (1,11,5),(2,6,7),(3,13,9),(12,7,16),(14,3,25),(19,18,22),(23,13,29),(24,4,28)
the skyline, shown on the right, is represented by the sequence:
(1,11,3,13,9,0,12,7,16,3,19,18,22,3,23,13,29,0)
这里写图片描述
大意:
由於高速繪圖電腦工作站的出現,CAD(computer-aided design)和其他領域(CAM,VLSI設計)都充分使用這些電腦的長處。而在本問題中,你必須幫助建築師,根據他所提供給你都市中建築物的位置,你得幫他找出這些建築物的空中輪廓(skyline)。為了使問題容易處理一些,所有的建築物都是矩形的,並且都建築在同一個平面上。你可以把這城市看成一個二度平面空間。每一棟建築物都以(Li Hi Ri)這樣的序列來表示。其中Li 和 Ri分別是該建築物左邊和右邊的位置,Hi則是建築物的高度。下方左圖就是(1,11,5), (2,6,7), (3,13,9), (12,7,16), (14,3,25), (19,18,22), (23,13,29), (24,4,28)這八棟建築物的位置圖。而你的任務就是畫出這些建築物所構成的輪廓,並且以(1, 11, 3, 13, 9, 0, 12, 7, 16, 3, 19, 18, 22, 3, 23, 13, 29, 0)這樣的序列來表示如下方右圖的輪廓。

Input

只有一組測試資料。

每列有一棟建築物的資料。建築物不會超過5000棟。所有的數字都小於10000。並且建築物已按照Li排好序。

Output

輸出為描述建築物輪廓的向量。在輪廓向量(v1,v2,v3,……,vn-1,vn)中,在i為奇數的情形下,vi表示一條垂直線(x座標),在i為偶數的情形下,vi表示一條水平線(高度)。輪廓向量就像一隻蟲從最左邊建築物走起,沿著輪廓路徑水平及垂直的行走的路徑。所以最後輪廓向量的最後一個數一定為0。

#include<bits/stdc++.h>
using namespace std;

//fstream in,out;
int ans[10001];
int main()
{
    ios::sync_with_stdio(false);
//  in.open("data.txt");
//  out.open("input.txt");
    string s;
    stringstream ss;
    int rside,lside;
    rside=0,lside=INT_MAX;
    memset(ans,0,sizeof(ans));
    while(getline(cin,s))
    {
        int l,h,r;
        ss.clear();
        ss<<s;
        ss>>l>>h>>r;
        rside=max(rside,r);
        lside=min(lside,l);
        for(int i=l;i<r;i++)
        {
            if(ans[i]<h)
                ans[i]=h;
        }
    }
    for(int i=lside;i<rside;i++)
    {
        if(ans[i]!=ans[i-1])
        {
            if(ans[i]==0)
                cout<<i<<" "<<0<<" ";
            else
                cout<<i<<" "<<ans[i]<<" ";
        }
    }
    cout<<rside<<" "<<0<<endl;
//  in.close();
//  out.close();
    return 0;
}

解答:
很简单的一道题,由于有一个小的注意点很重要,所以记录下来了。这里给出了数据范围是1到10000,所以直接开辟一个10000的区间用来记录这个区间最高点即可。这里注意要记录左闭右开!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值