poj2926Requirements (曼哈顿距离)

Description

An undergraduate student, realizing that he needs to do research to improve his chances of being accepted to graduate school, decided that it is now time to do some independent research. Of course, he has decided to do research in the most important domain: the requirements he must fulfill to graduate from his undergraduate university. First, he discovered (to his surprise) that he has to fulfill 5 distinct requirements: the general institute requirement, the writing requirement, the science requirement, the foreign-language requirement, and the field-of-specialization requirement. Formally, a requirement is a fixed number of classes that he has to take during his undergraduate years. Thus, for example, the foreign language requirement specifies that the student has to take 4 classes to fulfill this requirement: French I, French II, French III, and French IV. Having analyzed the immense multitude of the classes that need to be taken to fulfill the different requirements, our student became a little depressed about his undergraduate university: there are so many classes to take…

Dejected, the student began studying the requirements of other universities that he might have chosen after high school. He found that, in fact, other universities had exactly the same 5 requirements as his own university. The only difference was that different universities had different number of classes to be satisfied in each of the five requirement.

Still, it appeared that universities have pretty similar requirements (all of them require a lot of classes), so he hypothesized that no two universities are very dissimilar in their requirements. He defined the dissimilarity of two universities X and Y as |x1 − y1| + |x2 − y2| + |x3 − y3| + |x4 − y4| + |x5 − y5|, where an xi (yi) is the number of classes in the requirement i of university X (Y) multiplied by an appropriate factor that measures hardness of the corresponding requirement at the corresponding university.

Input

The first line of the input file contains an integer N (1 ≤ N ≤ 100 000), the number of considered universities. The following N lines each describe the requirements of a university. A university X is described by the five non-negative real numbers x1 x2 x3 x4 x5.

Output

On a single line, print the dissimilarity value of the two most dissimilar universities. Your answer should be rounded to exactly two decimal places.

Sample Input

3
2 5 6 2 1.5
1.2 3 2 5 4
7 5 3 2 5

Sample Output

12.80

题意:在五维坐标系下求n个点中两个点的最大曼哈顿距离。

思路:以二维坐标系为例,(x1,y1)(x2,y2)之间的距离为|x1-x2|+|y1-y2|,可能取±(x1-x2)±(y1-y2),且其他情况下算出来的关于这两个点的最大距离肯定比正确算曼哈顿距离的值小(去掉绝对值符号了)。我们可以把同一个坐标的放在一起,变为(±x1±y1)-(±x2±y2)。(注:这里x1和x2前面的符号是一致的,y1和y2前面的符号是一致的,这样才能保证对应值仍保持相减关系)。所以我们只要枚举每一维的分量前面的符号即可,然后求出每一个符号分量状态的最大值和最小值的差,更新最大值就行。

下面给出n维的模板(时间复杂度为O(n*dem*2^dem)


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 100050
#define dem 5
struct node{
    double p[dem+1];
}a[maxn];
double maxx[1<<dem],minx[1<<dem];

int main()
{
    int n,m,i,j,state,t;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1;i<=n;i++){
            for(j=1;j<=dem;j++){
                scanf("%lf",&a[i].p[j]);
            }
        }
        for(state=0;state<(1<<dem);state++){
            maxx[state]=-inf;
            minx[state]=inf;
        }
        double ans=0;
        for(state=0;state<(1<<dem);state++){
            for(i=1;i<=n;i++){
                double cnt=0;
                for(t=1;t<=dem;t++){
                    if(state&(1<<(t-1)) ){
                        cnt+=a[i].p[t];
                    }
                    else{
                        cnt-=a[i].p[t];
                    }
                }
                maxx[state]=max(maxx[state],cnt);
                minx[state]=min(minx[state],cnt);
            }
            ans=max(ans,maxx[state]-minx[state]);
        }
        printf("%.2f
",ans);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/herumw/p/9464553.html