专栏文章

题解:P11848 [TOIP 2023] 房屋推荐

P11848题解参与者 1已保存评论 0

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
0 条
当前快照
1 份
快照标识符
@miq04p6z
此快照首次捕获于
2025/12/03 20:47
3 个月前
此快照最后确认于
2025/12/03 20:47
3 个月前
查看原文
不要再怀疑我是不是 AI 了!!!

算法思路

这道题目要求我们根据房屋到最近地铁站的距离、租金和编号进行多条件排序。可以通过以下步骤解决这道题目:
  1. 最近距离计算:对每个房屋遍历所有地铁站,计算欧式距离平方的最小值(防止浮点运算)。
  2. 稳定排序:使用自定义排序规则依次比较距离、租金、编号。
  3. 输入输出优化:使用快速输入输出方法处理大规模数据。

复杂度分析

  • 时间复杂度:O(nm+nlogn)O(nm + n\log n),其中 nn 是房屋数量,mm 是地铁站数量。每个房屋需遍历所有地铁站计算距离,最后进行快速排序。
  • 空间复杂度:O(n)O(n),需要存储所有房屋的信息,nn 意思如上。

C++ 代码实现

CPP
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;

struct House {
    int a, b, r, id;
    ll min_dist_sq = 9e18; // 初始化为极大值
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    // 读取输入
    int n, m;
    cin >> n >> m;
    
    vector<House> houses(n);
    for (int i = 0; i < n; ++i) {
        auto& h = houses[i];
        cin >> h.a >> h.b >> h.r;
        h.id = i + 1; // 编号从1开始
    }
    
    vector<pair<int, int>> stations(m);
    for (auto& s : stations) {
        cin >> s.first >> s.second;
    }
    
    // 计算每个房屋到所有地铁站的最小距离平方
    for (const auto& [c, d] : stations) {
        for (auto& h : houses) {
            ll dx = h.a - c;
            ll dy = h.b - d;
            ll dist_sq = dx*dx + dy*dy;
            if (dist_sq < h.min_dist_sq) {
                h.min_dist_sq = dist_sq;
            }
        }
    }
    
    // 自定义排序规则
    sort(houses.begin(), houses.end(), [](const House& x, const House& y) {
        if (x.min_dist_sq != y.min_dist_sq) 
            return x.min_dist_sq < y.min_dist_sq;
        if (x.r != y.r)
            return x.r < y.r;
        return x.id < y.id;
    });
    
    // 输出结果
    for (const auto& h : houses) {
        cout << h.id << '\n';
    }
    
    return 0;
}

关键点

  • 避免浮点运算:通过比较距离平方替代实际距离,既避免精度误差又提升运算速度。
  • 快速输入输出:使用了 ios::sync_with_stdio(false)cin.tie(0) 加速输入输出。
  • 原地更新最小值:遍历地铁站时直接更新最小距离,不用存储中间结果。

示例分析

以题目样例 11 输入为例:
CPP
3 3
2 0 11000
5 0 12000
3 3 10000
1 3
3 0
5 3
计算过程:
  1. 房屋 11 到地铁站 22 的平方距离最小为 11
  2. 房屋 33 到两个地铁站的平方距离均为 44,但租金更优。
  3. 最终排序结果为 1 → 3 → 2

评论

0 条评论,欢迎与作者交流。

正在加载评论...