POJ 2926 Requirements【最远曼哈顿距离】
来源:互联网 发布:杨振宁 国籍 知乎 编辑:程序博客网 时间:2024/04/28 05:55
n个5维坐标的点,求这n个点中曼哈顿距离的最大值。
暴力枚举肯定超时。
只考虑二维空间上两个坐标之间的曼哈顿距离(x1, y1) 和 (x2, y2),|x1-x2| +|y1-y2|去掉绝对值符号后共有下列四种情况
(x1-x2) + (y1-y2), (x1-x2) + (y2-y1), (x2-x1) + (y1-y2), (x2-x1) + (y2-y1)
转化一下:
(x1+y1) - (x2+y2), (x1-y1) - (x2-y2), (-x1+y1) - (-x2+y2), (-x1-y1) - (-x2-y2)
显然,任意给两个点,我们分别计算上述四种情况,那么最大值就是曼哈顿距离。
转化后,“-”号两侧的坐标形式是一样的。维数为5,因此我们可以用二进制枚举。
最大曼哈顿距离 = max{每种情况下的最大值-最小值}
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;#define N 100003#define inf 1e100double a[N][5];int n;int main() { while (scanf("%d", &n) == 1) { for (int i=0; i<n; i++) for (int j=0; j<5; j++) scanf("%lf", &a[i][j]); double ans = 0, mi, mx, t; for (int s=0; s<(1<<5); s++) { mi = inf, mx = -inf; for (int i=0; i<n; i++) { t = 0; for (int j=0; j<5; j++) if ((1<<j) & s) t += a[i][j]; else t -= a[i][j]; mi = min(mi, t); mx = max(mx, t); } ans = max(ans, mx-mi); } printf("%.2lf\n", ans); } return 0;}
- POJ 2926 Requirements【最远曼哈顿距离】
- [poj 2926]Requirements[最远曼哈顿距离]
- poj 2926 Requirements N维最远曼哈顿距离
- 最远曼哈顿距离小结 poj 2926 Requirements&hdu 4666 Hyperspace
- 最远曼哈顿距离小结 poj 2926 Requirements&hdu 4666 Hyperspace
- POJ 2926 Requirements(多维最远曼哈顿距离)
- POJ 2926 Requirements (多维最远曼哈顿距离)
- poj 2926 Requirements 【5维点集最远曼哈顿距离】
- POJ 2926 Requirements (空间最远曼哈顿距离)
- ]Requirements[最远曼哈顿距离]
- poj 2926 Requirements (曼哈顿距离)
- zoj2688 Requirements 曼哈顿距离
- 最远(近)曼哈顿距离
- 最远曼哈顿距离
- 【最大曼哈顿距离】POJ2926 Requirements
- HDU4666+POJ2926【最远曼哈顿距离】
- HDU-4666 最远曼哈顿距离
- hdu4666--Hyperspace--最远曼哈顿距离
- 数据绑定时出现"这将导致集合中的两个绑定绑定到同一个属性"错误.
- LeetCode-Same Tree
- ids for this class must be manually assigned before calling save()
- IP数据报格式(网络层简介)
- SQL 2005 完全卸载
- POJ 2926 Requirements【最远曼哈顿距离】
- Android NDK学习与环境搭建
- linux下搭建datagurad(物理方式)
- HDU 4671 Backup Plan
- JDK 环境变量配置
- 学生信息管理系统之恍然大悟(2)
- Android不同分辨率的Drawable资源配置
- JAVA 排序算法
- 函数中分配内存的问题