221 - Urban Elevations (UVA)

题目链接如下:

Online Judge

首先,我的代码虽然AC了,但是是有问题的,uva的测试数据太水了所以侥幸通过而已。因为题目要求的数据是实数而非整数,我的代码是按所有数据都是整数来暴力做的......但因为刘汝佳的代码写得太好了,我就懒得再抄一遍了;所以就把自己有问题的代码贴在这里。

正确的解法在这里(也是刘汝佳的思路):UVA ~ 221 ~ Urban Elevations (离散化)-CSDN博客

cpp 复制代码
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
// #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<building> vec;
std::vector<int> 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<int> 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<int> res;
        std::set<int> 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;
}
相关推荐
Gyoku Mint7 分钟前
深度学习×第4卷:Pytorch实战——她第一次用张量去拟合你的轨迹
人工智能·pytorch·python·深度学习·神经网络·算法·聚类
葫三生1 小时前
如何评价《论三生原理》在科技界的地位?
人工智能·算法·机器学习·数学建模·量子计算
拓端研究室3 小时前
视频讲解:门槛效应模型Threshold Effect分析数字金融指数与消费结构数据
前端·算法
随缘而动,随遇而安5 小时前
第八十八篇 大数据中的递归算法:从俄罗斯套娃到分布式计算的奇妙之旅
大数据·数据结构·算法
IT古董6 小时前
【第二章:机器学习与神经网络概述】03.类算法理论与实践-(3)决策树分类器
神经网络·算法·机器学习
水木兰亭9 小时前
数据结构之——树及树的存储
数据结构·c++·学习·算法
Jess0710 小时前
插入排序的简单介绍
数据结构·算法·排序算法
老一岁10 小时前
选择排序算法详解
数据结构·算法·排序算法
xindafu10 小时前
代码随想录算法训练营第四十二天|动态规划part9
算法·动态规划
xindafu10 小时前
代码随想录算法训练营第四十五天|动态规划part12
算法·动态规划