Hyperspace
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
However, the device is in test phase, often in a unstable state. Mr.Smith worried that it may cause an explosion while testing it. The energy of the device is related to the maximum manhattan distance among particle.
Particles may appear and disappear any time. Mr.Smith wants to know the maxmium manhattan distance among particles when particle appears or disappears.
In each case: In the first line, there are two integer q(number of particle appear and disappear event, ≤60000) and k(dimensions of the hyperspace that the hyperspace the device generated, ≤5). Then follows q lines. In each line, the first integer ‘od’ represents the event: od = 0 means this is an appear
event. Then follows k integer(with absolute value less then 4 × 10 7). od = 1 means this is an disappear event. Follows a integer p represents the disappeared particle appeared in the pth event.
求最大曼哈顿距离,对于一个n维的空间,其中两点的曼哈顿距离为:|x1-y1|+|x2-y2|+|x3-y3|+|x4-y4|+……+|xn-yn|
(两点的坐标分别为(x1,x2,……,xn)、(y1,y2,……,yn))
以下以二维平面为例研究:
设距离最远的两点为i,j,可知所求的最大距离必定有以下四种形式之一:
|X1-Y1|+|X2-Y2|+...的最大值必然是±(X1-Y1)±(X2-Y2)±..之一
假如只有2个点 :坐标为 (x11 ,x12) (x21 ,x22)
Answer=Max{
(x11+x12)-(y21+y22) ,
(-x11+x12)-(-y21+y22) ,
(x11-x12)-(y21-y22) ,
(-x11-x12)-(-y21-y22) ,
}
假如有3个点 : (x11 ,x12) (x21 ,x22) (x31, x32)
Answer=Max{
(x11+x12)-(y21+y22) , (x11+x12)-(y31+y32) , ..........
(-x11+x12)-(-y21+y22) , (-x11+x12)-(-y31+y32) ,............
(x11-x12)-(y21-y22) , (x11-x12)-(y31-y32) ,...............
(-x11-x12)-(-y21-y22) , (-x11-x12)-(-y31-y32) ,...............
}
由此,可以发现一个规律,即去绝对值之后把同一点的坐标放在一起,对应坐标的符号总是相同的。
假如我们用0表示负号,1表示正号,则(-xi+yi)与(xj-yj)两个括号内的符号可以表示为:01和10
当你多举几个例子之后,就会发现,对于一个确定的维数D,符号转化成的二进制数,它们的和总是一个定值,即2^d-1, 这就说明了,当我们知道了前一个点去绝对值之后的符号,就可以知道第二个点去绝对值后的符号是怎样的
于是只要对所有的点(xi,yi),依次计算出(xi+yi),(xi-yi),(-xi+yi),(-xi-yi)这四种形式,然后把每个点i算出来的这四种情况的最大值分别记录到一个数组max[]中,然后枚举每一种去绝对值的组合,组合后的最大值即为answer
#include <iostream> #include <string.h> #include <string> #include <algorithm> #include <stdio.h> #include <queue> #include <set> #include <limits.h> #include <fstream> #include <map> #include <math.h> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std ; const int size=60008 ; struct Me{ int N ,K ; bool used[size] ; struct Node{ int id ; int num ; Node(){} Node(int i ,int n):id(i),num(n){} friend bool operator <(const Node A ,const Node B){ return A.num<B.num ; } }; struct Node2{ int id ; int num ; Node2(){} Node2(int i ,int n):id(i),num(n){} friend bool operator <(const Node2 A ,const Node2 B){ return A.num>B.num ; } }; priority_queue<Node>big[1<<5] ; priority_queue<Node2>small[1<<5] ; Me(){} Me(int n, int k):N(n),K(k){} int get_max(){ int ans=0 ,Max_num ,Min_num; for(int i=0;i<(1<<K);i++){ Max_num=Min_num=0 ; while(!big[i].empty()){ Node now=big[i].top() ; if(used[now.id]){ big[i].pop() ; continue ; } else{ Max_num=now.num ; break ; } } while(!small[i].empty()){ Node2 now=small[i].top() ; if(used[now.id]){ small[i].pop() ; continue ; } else{ Min_num=now.num; break ; } } ans=Max(ans,Max_num-Min_num) ; } return ans ; } void gao_qi(){ memset(used,0,sizeof(used)) ; int kind ,id; int x[5] ; for(int pth=1;pth<=N;pth++){ scanf("%d",&kind) ; if(kind==1){ scanf("%d",&id) ; used[id]=1 ; printf("%d\n",get_max()) ; } else{ for(int i=0;i<K;i++) scanf("%d",&x[i]) ; int num ,sum; for(int i=0;i<(1<<K);i++){ num=i ; sum=0 ; for(int j=0;j<K;j++){ if(num&1) sum-=x[j] ; else sum+=x[j] ; num>>=1 ; } big[i].push(Node(pth,sum)) ; small[i].push(Node2(pth,sum)) ; } printf("%d\n",get_max()) ; } } } }; int main(){ int n , k ; while(scanf("%d%d",&n,&k)!=EOF){ Me me(n,k) ; me.gao_qi() ; } return 0 ; }
Time Limit: 5000MS | Memory Limit: 65536K | |
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
#include <iostream> #include <string.h> #include <string> #include <algorithm> #include <stdio.h> #include <queue> #include <set> #include <limits.h> #include <fstream> #include <map> #include <math.h> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std ; const int size=10008 ; struct Me{ int N ; double Max_ans[1<<5] ; double Min_ans[1<<5] ; Me(){} Me(int n):N(n){} double gao_qi(){ double x[5] ,sum ,ans; int num; for(int cas=1;cas<=N;cas++){ for(int i=0;i<5;i++) scanf("%lf",&x[i]) ; for(int i=0;i<(1<<5);i++){ num=i ; sum=0 ; for(int j=0;j<5;j++){ if(num&1) sum+=x[j] ; else sum-=x[j] ; num>>=1 ; } if(cas==1){ Max_ans[i]=sum ; Min_ans[i]=sum ; } else{ Max_ans[i]=Max(Max_ans[i],sum) ; Min_ans[i]=Min(Min_ans[i],sum) ; } } } ans=Max_ans[0]-Min_ans[0] ; for(int i=1;i<(1<<5);i++) ans=Max(ans,Max_ans[i]-Min_ans[i]) ; return ans ; } }; int main(){ int n ; while(cin>>n){ Me me(n) ; printf("%.2lf\n",me.gao_qi()) ; } return 0 ; }