题目链接如下:
Online Judge
正确的解法在这里(也是刘汝佳的思路):UVA ~ 221 ~ Urban Elevations (离散化)-CSDN博客
我开始的代码如下(代码虽然AC了,但是是有问题的,uva的测试数据太水了所以侥幸通过而已。因为题目要求的数据是实数而非整数,我的代码是按所有数据都是整数来暴力做的……):
#include
#include
#include
#include
// #define debug
const int maxx = 1000000;
struct building{
int x, y, width, depth, height, number;
building(){}
building(int _x, int _y, int _width, int _depth, int _height): x(_x), y(_y), width(_width), depth(_depth), height(_height){}
};
int n, x, y, width, depth, height, kase = 0;
std::vector vec;
std::vector ans[maxx];
bool cmp1(const int &a, const int &b){
return vec[a - 1].y < vec[b - 1].y;
}
bool cmp2(const int &a, const int &b){
return vec[a - 1].x != vec[b - 1].x ? vec[a - 1].x < vec[b - 1].x : (vec[a - 1].y < vec[b - 1].y);
}
int main(){
#ifdef debug
freopen("1.txt", "r", stdin);
freopen("2.txt", "w", stdout);
#endif
while(scanf("%d", &n) == 1 && n){
printf("%s", kase ? "\n" : "");
printf("For map #%d, the visible buildings are numbered as follows:\n", ++kase);
std::vector tmp;
fill(ans, ans + maxx, tmp);
int maxWidth = 0;
int minWidth = 10000000;
vec.clear();
for(int i = 1; i <= n; ++i){
scanf("%d %d %d %d %d", &x, &y, &width, &depth, &height);
vec.push_back(building(x, y, width, depth, height));
vec.back().number = i;
maxWidth = std::max(maxWidth, x + width);
minWidth = std::min(minWidth, x);
for(int j = x; j < x + width; ++j){
ans[j].push_back(i);
}
}
for(int i = minWidth; i < maxWidth; ++i){
sort(ans[i].begin(), ans[i].end(), cmp1);
int height = 0;
for(int j = 0; j < ans[i].size(); ++j){
if(vec[ans[i][j] - 1].height > height){
height = vec[ans[i][j] - 1].height;
} else{
ans[i][j] = 0;
}
}
}
std::vector res;
std::set st;
for(int i = minWidth; i < maxWidth; ++i){
for(int j = 0; j < ans[i].size(); ++j){
if(ans[i][j] && st.find(ans[i][j]) == st.end()){
res.push_back(ans[i][j]);
st.insert(ans[i][j]);
}
}
}
sort(res.begin(), res.end(), cmp2);
for(int i = 0; i < res.size(); ++i){
printf("%d%s", res[i], i == res.size() - 1 ? "\n" : " ");
}
}
#ifdef debug
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
后来把刘汝佳的想法和我原先的想法综合了一下,改写成下面的代码:
#include
#include
#include
#include
// #define debug
struct building{
double x, y, width, depth, height;
building(double _x, double _y, double _width, double _depth, double _height): x(_x), y(_y), width(_width), depth(_depth), height(_height){}
};
double x, y, width, depth, height;
int n, kase = 0;
std::set pts;
std::vector vec;
bool cmp1(const int &a, const int &b){
return vec[a].y < vec[b].y;
}
bool cmp2(const int &a, const int &b){
return vec[a].x != vec[b].x ? vec[a].x < vec[b].x : (vec[a].y < vec[b].y);
}
int main(){
#ifdef debug
freopen("1.txt", "r", stdin);
freopen("2.txt", "w", stdout);
#endif
while(scanf("%d", &n) == 1 && n){
printf("%s", kase ? "\n" : "");
printf("For map #%d, the visible buildings are numbered as follows:\n", ++kase);
vec.clear();
pts.clear();
for(int i = 1; i <= n; ++i){
scanf("%lf %lf %lf %lf %lf", &x, &y, &width, &depth, &height);
vec.push_back(building(x, y, width, depth, height));
pts.insert(x);
pts.insert(x + width);
}
std::vector res;
double start = *(pts.begin());
for(auto it = ++pts.begin(); it != pts.end(); ++it){
double h = 0.0;
std::vector temp;
double middle = (start + *it) / 2;
for(int i = 0; i < vec.size(); ++i){
if((vec[i].x < middle && vec[i].x + vec[i].width > middle)){
temp.push_back(i);
}
}
sort(temp.begin(), temp.end(), cmp1);
for(int i = 0; i < temp.size(); ++i){
if(vec[temp[i]].height > h){
h = vec[temp[i]].height;
if(find(res.begin(), res.end(), temp[i]) == res.end()){
res.push_back(temp[i]);
}
}
}
start = *it;
}
sort(res.begin(), res.end(), cmp2);
for(int i = 0; i < res.size(); ++i){
printf("%d%s", res[i] + 1, i == res.size() - 1 ? "\n" : " ");
}
}
#ifdef debug
fclose(stdin);
fclose(stdout);
#endif
return 0;
}