HDU 4666 Hyperspace (2013多校7 1001题 最远曼哈顿距离)

Hyperspace

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

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 × 10
7). 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
2013 Multi-University Training Contest 7  

 

Recommend zhuyuanchen520  

 

 

 

 

经典的求最远曼哈顿距离。

 

可以看相应的论文:2009年国家集训队武森论文

 

其实就是维护(1<<k)个堆的最大值和最小值。

可以参考POJ 2926

 

我用multiset实现的。

可以使用map或者优先队列

 1 /* **********************************************
 2 Author      : kuangbin
 3 Created Time: 2013/8/13 18:25:38
 4 File Name   : F:\2013ACM练习\2013多校7\1001.cpp
 5 *********************************************** */
 6 
 7 #include <stdio.h>
 8 #include <string.h>
 9 #include <iostream>
10 #include <algorithm>
11 #include <vector>
12 #include <queue>
13 #include <set>
14 #include <map>
15 #include <string>
16 #include <math.h>
17 #include <stdlib.h>
18 using namespace std;
19 int a[60010][10];
20 multiset<int>mst[1<<5];
21 
22 int main()
23 {
24     //freopen("in.txt","r",stdin);
25     //freopen("out.txt","w",stdout);
26     int q,k;
27     while(scanf("%d%d",&q,&k)==2)
28     {
29         for(int i = 0;i < (1<<k);i++)
30             mst[i].clear();
31         int od,x;
32         for(int i = 1;i <= q;i++)
33         {
34             scanf("%d",&od);
35             if(od == 0)
36             {
37                 for(int j = 0;j < k;j++)
38                     scanf("%d",&a[i][j]);
39                 for(int j = 0; j < (1<<k); j++)
40                 {
41                     int s = 0;
42                     for(int t = 0; t < k;t++)
43                         if(j & (1<<t))
44                             s += a[i][t];
45                         else s -= a[i][t];
46                     mst[j].insert(s);
47                 }
48             }
49             else
50             {
51                 scanf("%d",&x);
52                 for(int j = 0; j < (1<<k); j++)
53                 {
54                     int s = 0;
55                     for(int t = 0; t < k;t++)
56                         if(j & (1<<t))
57                             s += a[x][t];
58                         else s -= a[x][t];
59                     multiset<int>::iterator it = mst[j].find(s);
60                     mst[j].erase(it);
61                 }
62             }
63             int ans = 0;
64             for(int j = 0; j < (1<<k);j++)
65             {
66                 multiset<int>::iterator it = mst[j].end();
67                 it--;
68                 int t1 = (*it);
69                 it = mst[j].begin();
70                 int t2 = (*it);
71                 ans = max(ans,t1-t2);
72             }
73             printf("%d\n",ans);
74         }
75     }
76     return 0;
77 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/p/3255752.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞