hdu 4666:Hyperspace(最远曼哈顿距离 + STL使用)

Hyperspace

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1023    Accepted Submission(s): 492


Problem Description
The great Mr.Smith has invented a hyperspace particle generator. The device is very powerful. The device can generate a hyperspace. In the hyperspace, particle may appear and disappear randomly. At the same time a great amount of energy was generated.
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.
 

 

Input
The input contains several test cases, terminated by EOF.
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 × 107). od = 1 means this is an disappear event. Follows a integer p represents the disappeared particle appeared in the pth event.
 

 

Output
Each test case should contains q lines. Each line contains a integer represents the maximum manhattan distance among paticles.
 

 

Sample Input
10 2
0 208 403
0 371 -180
1 2
0 1069 -192
0 418 -525
1 5
1 1
0 2754 635
0 -2491 961
0 2954 -2516

 

 

Sample Output
0
746
0
1456
1456
1456
0
2512
5571
8922
 

 

Source
 

 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:  4822 4821 4820 4819 4818 
 
  最远曼哈顿距离 + STL库使用
  题意是已知q和n,q是请求的数量,n是平面点的维数。接下来输入q行请求,每行请求由2部分组成,分别是指令od和操作数,od=0代表加入点,od1代表删除点。如果od=0,后面会有n个整数,代表n维点的n个坐标值;如果od=1,代表删除点,删除的是第q条指令加入的点,而不是删除当前已有的第几个点。例如指令“1 5”,表示删除第5行的指令加入的点。
  不清楚最远曼哈顿距离怎么求的筒靴可以看这里:最远曼哈顿距离
  其实就是将绝对值去掉,移位,然后发现规律。多找几组例子对照着演算一下就容易明白了。
  明白基本原理了可以先看看一道最远曼哈顿距离的入门题:poj 2926:Requirements(最远曼哈顿距离,入门题)
  因为有删除操作,所以直接用数组会比较麻烦,这时候用STL中的映射表map和集合set就比较方便了。本来只想找一道类似的题,直接套我写的模板过的,结果找到这么一道还得用STL的题,表示很无奈,借机熟悉熟悉STL吧。好吧我承认我是参照着别人的代码才写出来。
  参考博客HDU 4666 Hyperspace
  代码:
 1 #include <iostream>
 2 #include <iomanip>
 3 #include <stdio.h>
 4 #include <map>
 5 #include <set>
 6 using namespace std;
 7 int main()
 8 {
 9     int i,j,k,q,dem;
10     while(scanf("%d%d",&q,&dem)!=EOF){
11         multiset <int> s[20];    //定义多重集合
12         multiset <int>::iterator it1,it2;
13         map <int,int> m[20];    //式子对点
14         int a[6];
15         for(i=1;i<=q;i++){
16             int od;
17             scanf("%d",&od);
18             if(od==0){    //添加操作
19                 for(j=0;j<dem;j++)
20                     scanf("%d",&a[j]);
21                 for(j=0;j<(1<<(dem-1));j++){    //用二进制形式遍历所有可能的运算情况
22                     int sum = 0;
23                     for(k=0;k<5;k++){    //因为是五维的,所以有4个运算符
24                         //提取当前运算符
25                         int t = j & 1<<k;    //1为+,0为-
26                         if(t) sum+=a[k];
27                         else sum-=a[k];
28                     }
29                     s[j].insert(sum);
30                     m[j][i] = sum;
31                 }
32             }
33             else{    //删除
34                 int t;
35                 scanf("%d",&t);
36                 for(j=0;j<(1<<(dem-1));j++){
37                     if(m[j].size()>0){
38                         s[j].erase(s[j].find(m[j][t]));
39                         m[j].erase(t);
40                     }
41                 }
42             }
43             int Max = 0;
44             for(j=0;j<(1<<(dem-1));j++){
45                 if(s[j].size()>0){
46                     it1 = s[j].begin();
47                     it2 = s[j].end();
48                     it2--;
49                     Max = *it2-*it1 > Max? *it2-*it1 : Max;
50                 }
51             }
52             printf("%d\n",Max);
53         }
54     }
55     return 0;
56 }

 

Freecode : www.cnblogs.com/yym2013

posted @ 2014-04-25 11:40  Freecode#  阅读(452)  评论(0编辑  收藏  举报