Hyperspace HDU - 4666 (最远曼哈顿距离模板题)

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

思路:和今年多校赛的一题很像,之前也分析过做法(二进制枚举加减法)。这题区别在于它是以查询的形式询问的,所以需要用set来记录之前已经计算过的(需要用multiset,允许重复的)。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
const int mod=1e9+7;
#define inf 0x3f3f3f3f
int q,k;
multiset<int> se[1<<5];
int a[maxn][10];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    while(scanf("%d%d",&q,&k)!=EOF)
    {
        for(int i=0;i<(1<<k);i++)
        {
            se[i].clear();
        }
        int op,x;
        for(int i=1;i<=q;i++)
        {
            scanf("%d",&op);
            if(op==0)
            {
                for(int j=0;j<k;j++)
                {
                    scanf("%d",&a[i][j]);
                }
                for(int j=0;j<(1<<k);j++)
                {
                    int s=0;
                    for(int t=0;t<k;t++)
                    {
                        if(j&(1<<t))
                        {
                            s+=a[i][t];
                        }
                        else
                        {
                            s-=a[i][t];
                        }
                    }
                    se[j].insert(s);
                }
            }
            else
            {
                scanf("%d",&x);//对于删除点,需要去除每个和它有关的状态
                for(int j=0;j<(1<<k);j++)
                {
                    int s=0;
                    for(int t=0;t<k;t++)
                    {
                        if(j&(1<<t))
                        {
                            s+=a[x][t];
                        }
                        else
                        {
                            s-=a[x][t];
                        }
                    }
                    multiset<int>::iterator it=se[j].find(s);
                    se[j].erase(it);
                }
            }
            int ans=0;
            for(int j=0;j<(1<<k);j++)
            {
                multiset<int>::iterator it=se[j].end();
                it--;
                int t1=*(it);
                it=se[j].begin();
                int t2=*it;
                ans=max(ans,t1-t2);
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/82353957