pku 2926 Requirements 多维曼哈顿距离
题意:
给你5维的n个点的坐标,求其中两点之间曼哈顿距离最大。
思路:
多维曼哈顿距离的模板题目
|x1-x'1|+|x2-x'2|+... ...+|xm-x'm| 去掉绝对值后x,x'分别都有2^m种状态,枚举每种状态下的最大值和最下值,然后找出差值最大的即可。
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <algorithm> #include <string> #include <set> #include <functional> #include <numeric> #include <sstream> #include <stack> #include <map> #include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define ll long long #define L(x) (x) << 1 #define R(x) (x) << 1 | 1 #define MID(l, r) (l + r) >> 1 #define Min(x, y) (x) < (y) ? (x) : (y) #define Max(x, y) (x) < (y) ? (y) : (x) #define E(x) (1 << (x)) #define iabs(x) (x) < 0 ? -(x) : (x) #define OUT(x) printf("%I64d\n", x) #define lowbit(x) (x)&(-x) #define Read() freopen("data.in", "r", stdin) #define Write() freopen("d.out", "w", stdout) #define M 100007 #define N 100007 using namespace std; const int inf = 0x7f7f7f7f; const int mod = 1000000007; struct node { double x[6]; }pt[N]; double maxx[1<<5],minx[1<<5]; int n,m;//n表示点数,m表示维数 void solve(int n,int dem) { int msk = 1<<dem; int i,j,k; for (i = 0; i < msk; ++i) { maxx[i] = -inf; minx[i] = inf; } for (i = 0; i < n; ++i) { for (j = 0; j < msk; ++j) { double s = 0; int tmsk = j; for (k = 0; k < dem; ++k) { if (tmsk&1) s += pt[i].x[k]; else s -= pt[i].x[k]; tmsk >>= 1; } if (maxx[j] < s) maxx[j] = s; if (minx[j] > s) minx[j] = s; } } double ans = 0; for (i = 0; i < msk; ++i) { ans = max(ans,maxx[i] - minx[i]); } printf("%.2f\n",ans); } int main() { int i,j; m = 5; while (~scanf("%d",&n)) { for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) { scanf("%lf",&pt[i].x[j]); } } solve(n,m); } return 0; }