poj 2926 Requirements N维最远曼哈顿距离

以前写过1~3维的最远曼哈顿距离,(本博客第一篇,代码奇丑)这次写N维,可作模版。

复杂度:O(n*2^m)  (n个点,m维)

原理: |x1-y1|+|x2-y2|+... ...+|xm-ym| 去掉绝对值后x、y分别都有2^m种状态,枚举之。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int dem=5;        //维数
const int maxxn=100005;
const double inf=1e200;
struct Point{
    double x[dem];
}p[maxxn];
int n;
double minx[1<<dem], maxx[1<<dem];

double solve(){
    int i, j, k, t, tmp=1<<dem;
    double s, ans=-inf;
    for(i=0; i<tmp; i++){
        minx[i]=inf;
        maxx[i]=-inf;
    }
    for(i=0; i<n; i++){
        for(j=0; j<tmp; j++){
            t=j;s=0;
            for(k=0; k<dem; k++){
                if(t&1)
                s+=p[i].x[k];
                else s-=p[i].x[k];
                t>>=1;
            }
            if(maxx[j]<s)maxx[j]=s;
            if(minx[j]>s)minx[j]=s;
        }
    }
    for(i=0; i<tmp; i++){
        if(maxx[i]-minx[i]>ans)
        ans=maxx[i]-minx[i];
    }
    return ans;
}
int main(){
    //freopen("1.txt", "r", stdin);
    int i, j;
    while(scanf("%d", &n)!=EOF){
        for(i=0; i<n; i++){
            for(j=0; j<dem; j++)
                scanf("%lf", &p[i].x[j]);
        }
        printf("%.2f\n", solve());
    }
    return 0;
}


 

 

### 关于曼哈顿距离算法题练习 以下是几个经典的涉及曼哈顿距离算法题目及其背景介绍: #### 题目一:OJ 第1005题 —— 计算曼哈顿距离 此题要求输入两个点的坐标 `(x1, y1)` 和 `(x2, y2)`,并计算它们之间的曼哈顿距离。公式为 `|x1 - x2| + |y1 - y2|`[^1]。 ```python def manhattan_distance(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) # 测试样例 print(manhattan_distance(1, 2, 4, 6)) # 输出应为7 ``` --- #### 题目二:LeetCode 曼哈顿距离问题 在 LeetCode 的某些题目中会涉及到曼哈顿距离的应用场景。例如给定一组点集合,找到某个特定条件下的最优解。具体实现可能需要遍历所有点对来计算每一对点间的曼哈顿距离[^2]。 ```python from itertools import combinations def total_manhattan_distances(points): total = 0 for (x1, y1), (x2, y2) in combinations(points, 2): total += abs(x1 - x2) + abs(y1 - y2) return total points = [(1, 2), (3, 4), (5, 6)] print(total_manhattan_distances(points)) # 输出总曼哈顿距离 ``` --- #### 题目三:洛谷 P3964 [TJOI2013] 松鼠聚会 这是一道典型的多曼哈顿距离应用题。题目描述了一群松鼠分布在二平面的不同位置上,目标是最小化所有松鼠移动到某一点所需的总曼哈顿距离之和[^3]。 解决方法通常基于 **分治策略** 或者通过分析曼哈顿距离性质得出结论:最终的目标点通常是所有点坐标的中位数位置。 ```python import statistics def min_total_distance(points): xs = sorted([point[0] for point in points]) ys = sorted([point[1] for point in points]) median_x = statistics.median(xs) median_y = statistics.median(ys) total_distance = sum(abs(point[0] - median_x) + abs(point[1] - median_y) for point in points) return int(total_distance) points = [[1, 1], [2, 2], [3, 3]] print(min_total_distance(points)) ``` --- #### 题目四:POJ 2926 Requirements曼哈顿最远点对 该问题是更高度上的扩展版本,即考虑 n 空间中的多个点,并找出任意两点多曼哈顿距离最大值。 由于暴力枚举复杂度较高,在实际比赛中往往采用一些优化技巧或者数据结构加速查找过程。 ```python def max_multidimensional_mhd(points): dimensions = len(points[0]) # 假设所有点具有相同度 max_dist = float('-inf') for i in range(len(points)): for j in range(i + 1, len(points)): dist = sum(abs(a - b) for a, b in zip(points[i], points[j])) if dist > max_dist: max_dist = dist return max_dist points = [ [1, 2, 3], [4, 5, 6], [-1, -2, -3] ] print(max_multidimensional_mhd(points)) # 打印最大曼哈顿距离 ``` --- #### 题目五:牛客网第八场多校赛 D 距离(三 BIT) 本题是一个更复杂的三曼哈顿最近点对问题,需要用到高级数据结构如树状数组(Binary Indexed Tree, BIT)。核心思路是对每个度分别处理前缀和后缀信息,从而快速查询满足条件的最佳匹配点。 代码实现较为复杂,建议深入研究相关资料后再尝试编码。 --- ### 总结 上述列举了几类不同难度级别的曼哈顿距离相关题目,涵盖了基础概念理解以及高阶优化技术等多个层面的知识点。希望这些例子能够帮助您更好地掌握这一领域的内容!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值