您的位置:首页 > 其它

算典05_例题_12_UVA-221

2017-04-20 15:22 375 查看

Urban Elevations

题意

如图所示,有n(n≤100)个建筑物。左侧是俯视图(左上角为建筑物编号,右下角为高度),右侧是从南向北看的正视图。



输入每个建筑物左下角坐标(即x、y坐标的最小值)、宽度(即x方向的长度)、深度(即y方向的长度)和高度(以上数据均为实数)

输出正视图中能看到的所有建筑物,按照左下角x坐标从小到大进行排序。左下角x坐标相同时,按y坐标从小到大排序。

题解

这一题相比前两题的复杂性真是直线下滑,终于不用做英语阅读理解了

这道题主要考查的是离散化的思想,知道了这一点,这道题基本上就有思路了

把所有x坐标排序去重,则任意两个相邻x坐标形成的区间具有相同属性,一个区间要么完全可见,要么完全不可见。这样,只需在这个区间里任选一个点(例如中点),就能判断出一个建筑物是否在整个区间内可见。如何判断一个建筑物是否在某个x坐标处可见呢?首先,建筑物的坐标中必须包含这个x坐标,其次,建筑物南边不能有另外一个建筑物也包含这个x坐标,并且不比它矮。

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstring>
#include <cstdio>
#include <cmath>
#define met(a, b) memset(a, b, sizeof(a));
#define IN freopen("in.txt", "r", stdin);
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 5;
const int INF = (1 << 31) - 1;

struct node {
int id;
double x, y, w, d, h;
bool operator < (const node& b) const {
return x < b.x || (x == b.x && y < b.y);
}
}b[maxn];

int n;
double x[maxn*2];

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

bool visible(int i, double mx) {
if(!cover(i, mx) ) return 0;
for(int k = 0; k < n; ++k) {
if(b[k].y < b[i].y && b[k].h >= b[i].h && cover(k, mx)) return 0;
}
return 1;
}

int main() {
#ifdef _LOCAL
IN;
#endif // _LOCAL

int kase = 0;
while(scanf("%d", &n) == 1 && n) {
for(int i = 0; i < n; ++i) {
scanf("%lf%lf%lf%lf%lf", &b[i].x, &b[i].y, &b[i].w, &b[i].d, &b[i].h);
x[i*2] = b[i].x; x[i*2+1] = b[i].x + b[i].w;
b[i].id = i+1;
}
sort(b, b+n);
sort(x, x+n*2);
int m = unique(x, x+n*2)-x;

if(kase++) printf("\n");
printf("For map #%d, the visible buildings are numbered as follows:\n%d",kase, b[0].id);
for(int i = 1; i < n; ++i) {
bool vis = 0;
for(int j = 0; j < m-1; ++j) {
if(visible(i, (x[j] + x[j+1])/2)){ vis = 1; break; }
}
if(vis) printf(" %d", b[i].id);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: