Hyperspace
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 637 Accepted Submission(s): 287
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
Recommend
zhuyuanchen520
1. 本质是枚举
2.相当于维护1<<5 个有序链表,用set实现
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <set>
#define M 1<<6
#define N 70000
using namespace std;
struct num
{
int pos[6];
} a[N];
class te
{
public:
int id,val;
te(int x1,int x2):id(x1),val(x2) {}
};
bool operator>(const te &t1,const te &t2)
{
return t1.val>t2.val;
}
int uv[6],n,k;
multiset< te,greater<te> >p[M];
int main()
{
//freopen("data.in","r",stdin);
int get();
while(scanf("%d %d",&n,&k)!=EOF)
{
for(int i=0; i<(1<<M); i++)
{
p[i].clear();
}
for(int i=0; i<=n-1; i++)
{
int tag;
scanf("%d",&tag);
if(tag==0)
{
for(int j=0; j<=k-1; j++)
{
scanf("%d",&a[i].pos[j]);
}
for(int j=0; j<(1<<k); j++)
{
int x = j;
for(int y=0; y<=k-1; y++)
{
uv[y] = x&1;
x=x>>1;
}
int s=0;
for(int y=0; y<=k-1; y++)
{
if(uv[y]==0)
{
s+=a[i].pos[y];
}else
{
s+=(-1*a[i].pos[y]);
}
}
p[j].insert(te(i,s));
}
int Max = get();
printf("%d\n",Max);
}else if(tag==1)
{
int w;
scanf("%d",&w);
for(int j=0; j<(1<<k); j++)
{
int x = j;
for(int y=0; y<=k-1; y++)
{
uv[y] = x&1;
x=x>>1;
}
int s=0;
for(int y=0; y<=k-1; y++)
{
if(uv[y]==0)
{
s+=a[w-1].pos[y];
}else
{
s+=(-1*a[w-1].pos[y]);
}
}
multiset<te>::iterator it1=(p[j].find(te(w-1,s)));
p[j].erase(it1);
}
int Max = get();
printf("%d\n",Max);
}
}
}
return 0;
}
int get()
{
int Max = 0;
for(int j=0; j<(1<<k); j++)
{
multiset<te>::iterator it1=p[j].begin();
multiset<te>::iterator it2=p[(1<<k)-1-j].begin();
if((*it1).id!=(*it2).id)
{
Max = max(Max,(*it1).val+(*it2).val);
}else
{
int x=(*it2).id;
int y = (*it2).val;
p[(1<<k)-1-j].erase(it2);
it2 = p[(1<<k)-1-j].begin();
if(it2!=p[(1<<k)-1-j].end())
{
Max = max(Max,(*it1).val+(*it2).val);
}
p[(1<<k)-1-j].insert(te(x,y));
}
}
return Max;
}