uva 221 Urban Elevations

本文介绍了一个名为UrbanElevations的问题解决方法。该方法通过离散化横坐标并排序来确定从特定视角可见的城市建筑。文章详细展示了使用C++实现的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:Urban Elevations


题意:给定一个城市地图,求从前向后看可以看到那些城市。


思路:离散化横坐标,再按横坐标排序,顺序输出。


注意:格式中有一个#,不能掉了!


代码:

#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
using namespace std;

struct HOUSE {
	int id;
	double x1,y1,x2,y2,h;
	bool operator <(const HOUSE& other) const {
		if(x1<other.x1||(x1==other.x1&&y1<other.y1)) return true;
		return false;
	}
	void in(int i) {
		id=i;
		scanf("%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&h);
		x2+=x1,y2+=y1;
	}
};

struct Pair {
	double y,h;
	int id;
	Pair() {}
	Pair(double one,double two,int three) {
		y=one,h=two,id=three;
	}
	bool operator <(const Pair& other) const {
		if(y<other.y) return true;
		return false;
	}
};

int n;
vector<HOUSE> vec;
int sum;
map<int,HOUSE> ID;
vector<int> print;

void init() {
	print.clear();
	ID.clear();
	vec.clear();
	map<int,double> mp;
	vector<double> xx;
	for(int i=1; i<=n; i++) {
		HOUSE t;
		t.in(i);
		vec.push_back(t);
		xx.push_back(t.x1),xx.push_back(t.x2);
	}
	sort(vec.begin(),vec.end());
	sort(xx.begin(),xx.end());
	int j=0;
	for(int i=0; i<xx.size(); i++) {
		if(!mp.count(xx[i])) mp[xx[i]]=++j;
	}
	for(int i=0; i<vec.size(); i++) {
		vec[i].x1=mp[vec[i].x1],vec[i].x2=mp[vec[i].x2];
		ID[vec[i].id]=vec[i];
	}
	sum=j;
}

void change() {
	bool use[20000]= {0};
	for(int i=1; i<sum; i++) {
		vector<Pair> col;
		for(int j=0; j<vec.size(); j++) {
			if(vec[j].x1<=i+0.5&&i+0.5<=vec[j].x2) {
				col.push_back(Pair(vec[j].y1,vec[j].h,vec[j].id));
			}
		}
		double h=-10000;
		sort(col.begin(),col.end());
		for(int j=0; j<col.size(); j++) {
			if(use[col[j].id]==false&&col[j].h>h) print.push_back(col[j].id),use[col[j].id]=true;
			h=max(h,col[j].h);
		}
	}
}

int main() {
	int T=0;
	while(scanf("%d",&n)&&n!=0) {
		if(T) printf("\n");
		init();
		change();
		printf("For map #%d, the visible buildings are numbered as follows:\n",++T);
		vector<HOUSE> t;
		for(int i=0; i<print.size(); i++) {
			t.push_back(ID[print[i]]);
		}
		sort(t.begin(),t.end());
		printf("%d",t[0].id);
		for(int i=1; i<t.size(); i++) {
			printf(" %d",t[i].id);
		}
		printf("\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值