poj 2926:Requirements(最远曼哈顿距离,入门题)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 3908 | Accepted: 1318 |
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 Xand 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
Source
#define inf 1e200
double a[100001][6]; //每一个点的5个坐标值
double GetManhattan(double p[][6],int n,int dem)
{
double ans = 0,Min,Max;
int i,j,k;
for(i=0;i<(1<<(dem-1));i++){ //用二进制形式遍历所有可能的运算情况
Min = inf,Max = -inf;
for(j=0;j<n;j++){ //遍历每一个点
double sum = 0;
for(k=0;k<5;k++){ //因为是五维的,所以有4个运算符
//提取当前运算符
int t = i & 1<<k; //1为+,0为-
if(t) sum+=p[j][k];
else sum-=p[j][k];
}
if(sum>Max) Max = sum;
if(sum<Min) Min = sum;
}
if(Max-Min>ans)
ans = Max - Min;
}
return ans;
}
1 #include<iostream>
2 #include<stdio.h>
3 #include<iomanip>
4 const double MAX=1e100;
5 const double MIN=-1e100;
6 using namespace std;
7 int n;
8 double fivemaxdistance() //三维点集最大曼哈顿距离
9 {
10 double max1=MIN,min1=MAX,max2=MIN,min2=MAX,max3=MIN,min3=MAX,max4=MIN,min4=MAX,
11 max5=MIN,min5=MAX,
12 max6=MIN,min6=MAX,
13 max7=MIN,min7=MAX,
14 max8=MIN,min8=MAX,
15 max9=MIN,min9=MAX,
16 max10=MIN,min10=MAX,
17 max11=MIN,min11=MAX,
18 max12=MIN,min12=MAX,
19 max13=MIN,min13=MAX,
20 max14=MIN,min14=MAX,
21 max15=MIN,min15=MAX,
22 max16=MIN,min16=MAX,x,y,z,k,l,ans;
23 for(int i=1;i<=n;++i)
24 {
25 scanf("%lf%lf%lf%lf%lf",&x,&y,&z,&k,&l);
26 if(x+y+z+k+l>max1) max1=x+y+z+k+l;
27 if(x+y+z+k+l<min1) min1=x+y+z+k+l;
28 if(x-y+z+k+l>max2) max2=x-y+z+k+l;
29 if(x-y+z+k+l<min2) min2=x-y+z+k+l;
30 if(x+y-z+k+l>max3) max3=x+y-z+k+l;
31 if(x+y-z+k+l<min3) min3=x+y-z+k+l;
32 if(x+y+z-k+l>max4) max4=x+y+z-k+l;
33 if(x+y+z-k+l<min4) min4=x+y+z-k+l;
34 if(x+y+z+k-l>max5) max5=x+y+z+k-l;
35 if(x+y+z+k-l<min5) min5=x+y+z+k-l;
36 if(x-y-z+k+l>max6) max6=x-y-z+k+l;
37 if(x-y-z+k+l<min6) min6=x-y-z+k+l;
38 if(x-y+z-k+l>max7) max7=x-y+z-k+l;
39 if(x-y+z-k+l<min7) min7=x-y+z-k+l;
40 if(x-y+z+k-l>max8) max8=x-y+z+k-l;
41 if(x-y+z+k-l<min8) min8=x-y+z+k-l;
42 if(x+y-z-k+l>max9) max9=x+y-z-k+l;
43 if(x+y-z-k+l<min9) min9=x+y-z-k+l;
44 if(x+y-z+k-l>max10) max10=x+y-z+k-l;
45 if(x+y-z+k-l<min10) min10=x+y-z+k-l;
46 if(x+y+z-k-l>max11) max11=x+y+z-k-l;
47 if(x+y+z-k-l<min11) min11=x+y+z-k-l;
48 if(x-y-z-k+l>max12) max12=x-y-z-k+l;
49 if(x-y-z-k+l<min12) min12=x-y-z-k+l;
50 if(x+y-z-k-l>max13) max13=x+y-z-k-l;
51 if(x+y-z-k-l<min13) min13=x+y-z-k-l;
52 if(x-y+z-k-l>max14) max14=x-y+z-k-l;
53 if(x-y+z-k-l<min14) min14=x-y+z-k-l;
54 if(x-y-z+k-l>max15) max15=x-y-z+k-l;
55 if(x-y-z+k-l<min15) min15=x-y-z+k-l;
56 if(x-y-z-k-l>max16) max16=x-y-z-k-l;
57 if(x-y-z-k-l<min16) min16=x-y-z-k-l;
58 }
59 ans=max1-min1;
60 if(max2-min2>ans) ans=max2-min2;
61 if(max3-min3>ans) ans=max3-min3;
62 if(max4-min4>ans) ans=max4-min4;
63 if(max5-min5>ans) ans=max5-min5;
64 if(max6-min6>ans) ans=max6-min6;
65 if(max7-min7>ans) ans=max7-min7;
66 if(max8-min8>ans) ans=max8-min8;
67 if(max9-min9>ans) ans=max9-min9;
68 if(max10-min10>ans) ans=max10-min10;
69 if(max11-min11>ans) ans=max11-min11;
70 if(max12-min12>ans) ans=max12-min12;
71 if(max13-min13>ans) ans=max13-min13;
72 if(max14-min14>ans) ans=max14-min14;
73 if(max15-min15>ans) ans=max15-min15;
74 if(max16-min16>ans) ans=max16-min16;
75 return ans;
76 }
77 int main()
78 {
79 while(cin>>n)
80 { //输入点数
81 cout<<setiosflags(ios::fixed)<<setprecision(2);
82 cout<<fivemaxdistance()<<endl;
83 }
84 return 0;
85 }
优化方法(用二进制方法遍历运算符):
1 #include <iostream>
2 #include <iomanip>
3 #include <stdio.h>
4 using namespace std;
5
6 #define inf 1e200
7 double a[100001][6]; //每一个点的5个坐标值
8 double GetManhattan(double p[][6],int n,int dem)
9 {
10 double ans = 0,Min,Max;
11 int i,j,k;
12 for(i=0;i<(1<<(dem-1));i++){ //用二进制形式遍历所有可能的运算情况
13 Min = inf,Max = -inf;
14 for(j=0;j<n;j++){ //遍历每一个点
15 double sum = 0;
16 for(k=0;k<5;k++){ //因为是五维的,所以有4个运算符
17 //提取当前运算符
18 int t = i & 1<<k; //1为+,0为-
19 if(t) sum+=p[j][k];
20 else sum-=p[j][k];
21 }
22 if(sum>Max) Max = sum;
23 if(sum<Min) Min = sum;
24 }
25 if(Max-Min>ans)
26 ans = Max - Min;
27 }
28 return ans;
29 }
30 int main()
31 {
32 int i,j,n;
33 while(cin>>n){
34 for(i=0;i<n;i++) //输入n个点
35 for(j=0;j<5;j++)
36 scanf("%lf",&a[i][j]);
37 //计算最短曼哈顿距离
38 cout<<setiosflags(ios::fixed)<<setprecision(2);
39 cout<<GetManhattan(a,n,5)<<endl;
40 }
41 return 0;
42 }
Freecode : www.cnblogs.com/yym2013