1. 程式人生 > >POJ 2926 Requirements (多維曼哈頓最遠距離)

POJ 2926 Requirements (多維曼哈頓最遠距離)

Requirements

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 4491   Accepted: 1570

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| + |x

2 − 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 xxxxx5.

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

Source

MIT Programming Contest 2005

題意:給你n(n<=1e5)個5維點的座標,讓你求曼哈頓距離最遠的兩個點的距離。

思路:算是個結論吧,首先,暴力列舉肯定超時。

於是我們先考慮二維空間上兩個座標之間的曼哈頓距離(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 <bits/stdc++.h>   //poj不支援萬能標頭檔案!!!
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
#include<iomanip>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=5e5+10;
double a[maxn][6];
double ans,tmp,cnt;
double mi,ma;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<5;j++)
        scanf("%lf",&a[i][j]);
    }
    ans=-inf;
    for(int k=0;k<(1<<5);k++)
    {
        mi=inf;ma=-inf;
        for(int i=0;i<n;i++)
        {
            double tmp=0;
            for(int j=0;j<5;j++)
            if(k&(1<<j))tmp+=a[i][j];
            else tmp-=a[i][j];
            ma=max(ma,tmp);
            mi=min(mi,tmp);
        }
        ans=max(ans,ma-mi);
    }
    cout<<fixed<<setprecision(2)<<ans<<endl;
    return 0;
}