uva 221 Urban Elevations

来源:互联网 发布:真的有淘宝王子? 编辑:程序博客网 时间:2024/06/06 19:49

题目: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;}

原创粉丝点击