zoj2688 Requirements Manhattan Distance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.