Urban Elevations UVA - 221离散化思想

题目链接:https://cn.vjudge.net/problem/UVA-221

在这里插入图片描述
在这里插入图片描述在这里插入图片描述
题目大意:
如图5-4所示,有n(n≤100)个建筑物。左侧是俯视图(左上角为建筑物编号,右下角为高度),右侧是从南向北看的正视图。

输入每个建筑物左下角坐标(即x、y坐标的最小值)、宽度(即x方向的长度)、深度(即y方向的长度)和高度(以上数据均为实数),输出正视图中能看到的所有建筑物,按照左下角x坐标从小到大进行排序。左下角x坐标相同时,按y坐标从小到大排序。

输入保证不同的x坐标不会很接近(即任意两个x坐标要么完全相同,要么差别足够大,不会引起精度问题)。

分析:把x坐标离散化,即把x坐标去重,便得到’‘相对连续’'的区间。
然后求出‘’连续区间‘’的长度(即为去重后不重复元素的长度),然后对每个建筑对区间遍历来查看是否可见。

照紫书的代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100+10;
struct Building {
    int id;
    double x,y,w,d,h;

    bool operator < (const Building& rhs) {
        return x < rhs.x || (x == rhs.x && y < rhs.y);
    }

};
struct Building building[maxn];
int n;

bool Cover(int i, int mx) {
    return building[i].x<=mx && (building[i].x+building[i].w)>=mx;
}

bool Visible(int i, int mx) {
    if(!Cover(i, mx)) return false;
    for(int k = 0; k < n; k++)
        if(building[i].y>building[k].y && building[i].h<=building[k].h && Cover(k,mx))
            return false;
    return true;
}

int main() {
    freopen("i.txt", "r", stdin);
    int rnd = 0;
    while(cin >> n && n) {
        //区间
        double x[maxn*2];
        for(int i = 0; i < n; i++) {
            building[i].id = i+1;
            cin >> building[i].x >> building[i].y >> building[i].w >> building[i].d >> building[i].h;
            x[i*2] = building[i].x, x[i*2+1] = building[i].x+building[i].w;
        }
        sort(building, building+n);
        sort(x, x+n*2);
        int cnt = unique(x, x+n*2) - x;
        if(rnd) cout << endl;
        cout << "For map #" << ++rnd << ", the visible buildings are numbered as follows:\n" << building[0].id;
        for(int i = 1; i < n; i++) {
            for(int j = 0; j < cnt-1; j++) {
                if(Visible(i, (x[j]+x[j+1])/2)) {
                    cout << " " << building[i].id;
                    break;
                }
            }
        }
        cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值