Topic Links:
zoj2688
Test instructions
Given n 5-D coordinates, find the maximum value of the Manhattan distance in these n coordinates.
Problem Solving Ideas:
Bare violence judgment Words time complexity is n^2, for n=10^5 of data amount, certainly will time out.
According to the two-dimensional coordinates of the Manhattan distance definition:
Dis (P1,P2) = ABS (X1-X2) + ABS (Y1-Y2);
If the absolute value is eliminated (i.e. the positive or negative of the enumeration symbol) There are four possibilities, namely the 2^k species (k=2)
Dis (P1,P2) = max{(x1+y1)-(X2+y2), (x1-y1)-(X2-y2), (-x1+y1)-(-x2+y2), (-x1-y1)-(-x2-y2)};
And in each case the x1,x2,x3 ... Same symbol, y1,y2,y3 .... Symbols are the same.
So. For 5-dimensional coordinates, we can enumerate all cases of 5 symbols (2^5) and save them.
Finally, the maximum distance can be compared with the same symbol. Time complexity is n*2^k (k=5)
Code:
#include <iostream> #include <cstdio> #include <cstring> #define MAXN 100050using namespace std;double V[MAXN][32];d ouble a[6];void dfs (int x,int level,int b,double sum) {if (level==6) {v[x][b]=sum; Return } dfs (X,level+1,b<<1,sum+a[level]); DFS (x,level+1, (b<<1) +1,sum-a[level]);} int main () {//Freopen ("In.txt", "R", stdin); int n; Double min1,max1; Double ans; while (~SCANF ("%d", &n) &&n) {ans=0.0; for (int. i=1;i<=n;i++) {for (int j=1;j<=5;j++) scanf ("%lf", &a[j]); DFS (i,1,0,0); } for (int i=0;i<32;i++) {max1=min1=v[1][i]; for (int j=1;j<=n;j++) {if (Max1<v[j][i]) max1=v[j][i]; if (Min1>v[j][i]) min1=v[j][i]; } if (ans<max1-min1) ans=max1-min1; } printf ("%.2lf\n", ans);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
zoj2688 Requirements Manhattan Distance