hdoj 4666 Hyperspace 【最远曼哈顿距离】

来源:互联网 发布:锁骨链一套淘宝 编辑:程序博客网 时间:2024/04/30 02:43

Hyperspace

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


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 20 208 4030 371 -1801 20 1069 -1920 418 -5251 51 10 2754 6350 -2491 9610 2954 -2516
 

Sample Output
074601456145614560251255718922
 

题意:在m维空间,有n个操作,0 代表加入一个点,1 代表删去一个点。对每一个操作输出两点间最远的曼哈顿距离。


思路:用一个multiset插入和删除就可以了。C++ vector 插入删除太慢。


AC代码:


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <string>#define INF (2000000000+10)#define eps 1e-8#define MAXN (60000+10)#define MAXM (600000+10)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while((a)--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1#define PI acos(-1.0)#pragma comment(linker, "/STACK:102400000,102400000")#define fi first#define se secondusing namespace std;typedef pair<int, int> pii;int a[MAXN][5];multiset<int> G[1<<5];multiset<int> ::iterator it;int main(){    int n, m;    while(scanf("%d%d", &n, &m) != EOF)    {        for(int i = 0; i < (1<<m); i++) G[i].clear();        for(int i = 1; i <= n; i++)        {            int op; Ri(op);            if(op == 0)            {                for(int j = 0; j < m; j++) Ri(a[i][j]);                for(int s = 0; s < (1<<m); s++)                {                    int temp = 0;                    for(int j = 0; j < m; j++)                    {                        if(s & (1<<j)) temp += a[i][j];                        else temp -= a[i][j];                    }                    G[s].insert(temp);                }            }            else            {                int x; Ri(x);                for(int s = 0; s < (1<<m); s++)                {                    int temp = 0;                    for(int j = 0; j < m; j++)                    {                        if(s & (1<<j)) temp += a[x][j];                        else temp -= a[x][j];                    }                    it = G[s].find(temp);                    G[s].erase(it);                }            }            int ans = 0;            for(int s = 0; s < (1<<m); s++)            {                it = G[s].end(); it--;                int Max = *it;                it = G[s].begin();                int Min = *it;                ans = max(ans, Max - Min);            }            Pi(ans);        }    }    return 0;}




0 0