XDOJ1015--Requirements

来源:互联网 发布:预算计价软件 编辑:程序博客网 时间:2024/05/17 06:44
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
There are multiple test cases in the input file. Every case begins with 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.The file is end of EOF.
Output
For each case,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

解题思路
很明显如果用穷举的方式,有C(n,2)种组合的情况,肯定时间会超时。最后网上搜索后才明白可以这是曼哈顿距离的扩展。
二维的曼哈顿距离就是|x1 − y1| + |x2 − y2| + |x3 − y3| + |x4 − y4| + |x5 − y5|
关键在于下面的变幻,如何把多维的曼哈顿距离转换为一维的情况:
显然|x1 − x2| + |y1 − y2|去掉绝对值后我们可以得到四种式子,x1 + y1 - x2 − y2,x1 − y1 - x2 + y2,y1 - xx2 − y2,- y1 - xy2 + x2,即得x1 + y1 - (x2 + y2),x1 − y1 - (x2 - y2), y1 - x1 − (yx2),- y1 - x−(− y2 − x2)
大家很容易就能注意到,四个等式的前后半项的符号是相同的。那么我们就可以用00,01,10,11表示这四种状态,0代表加,1代表减,就可以构造一个数组,标号便可以是0,1,2,3。如果我们找到了四种式子的最大值,再取四个式子中的最大值,不就可以得到答案了。而求每种式子的最大值方法是将式子处理后的数据中的最大值-最小值。
对上述题目,是5维的情况,其去掉绝对值后会得到2^5=32种式子,从00000~11111,其中0可以代表加也可以代表减。


#include <iostream>#include <iomanip>using namespace std;const int N=100000;double req[33][N];const int D = 5;const int M = 32;int main(){    int n;    while(cin>>n)    {        double a[D];        for(int k=0;k<n;++k)        {            for(int i=0;i<D;++i)                cin>>a[i];            for(int i=0;i<M;++i)            {                req[i][k] = 0;                int p = i;                for(int j=0;j<D;++j)                {                    if(p&1)                        req[i][k] -= a[D-1-j];                    else                        req[i][k] += a[D-1-j];                    p >>= 1;                }            }        }        double maxDiff = 0.0;        for(int i=0;i<M;++i)        {            double maxT,minT;            minT = maxT = req[i][0];             for(int j=1;j<n;++j)             {                 if(req[i][j]>maxT)                    maxT = req[i][j];                 if(req[i][j]<minT)                    minT = req[i][j];             }             if(maxT-minT>maxDiff)                maxDiff = maxT-minT;        }        cout<<setiosflags(ios::fixed)<<setprecision(2)<<maxDiff<<endl;    }    return 0;}





0 0
原创粉丝点击