首页 > ACM题库 > HDU-杭电 > HDU 4666-Hyperspace-枚举-[解题报告]HOJ
2015
09-17

HDU 4666-Hyperspace-枚举-[解题报告]HOJ

Hyperspace

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

0
746
0
1456
1456
1456
0
2512
5571
8922

题意:

给定一些操作(0代表添加一个点,1代表删除一个点),求这些点的最远曼哈顿距离。

做法:

只考虑二维空间上两个坐标之间的曼哈顿距离(x1, y1) 和 (x2, y2),|x1-x2| +|y1-y2|去掉绝对值符号后共有下列四种情况

(x1-x2) + (y1-y2), (x1-x2) + (y2-y1), (x2-x1) + (y1-y2), (x2-x1) + (y2-y1)

转化一下:

(x1+y1) – (x2+y2), (x1-y1) – (x2-y2), (-x1+y1) – (-x2+y2), (-x1-y1) – (-x2-y2)

显然,任意给两个点,我们分别计算上述四种情况,那么最大值就是曼哈顿距离。

转化后,“-”号两侧的坐标形式是一样的。维数为5,因此我们可以用二进制枚举。

最大曼哈顿距离 = max{每种情况下的最大值-最小值};

用multiset存储每种情况的值。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
#define maxn 60002
int maps[maxn][6];
multiset<int>se[1<<5];
multiset<int>::iterator i1,i2;
int main()
{
    int i,j,leap,x,n,m,a;
    while(~scanf("%d%d",&n,&m))
    {
        for(i=0;i<(1<<5);i++)
            se[i].clear();
        for(i=1;i<=n;i++)
        {
            scanf("%d",&leap);
            if(leap==0)
            {
                for(j=0;j<m;j++)
                {
                    scanf("%d",&maps[i][j]);
                }
                for(a=0;a<(1<<m);a++)
                {
                    int as;
                    as=0;
                    for(j=0;j<m;j++)
                    {
                        if(a&(1<<j))as+=maps[i][j];
                        else as-=maps[i][j];
                    }
                    se[a].insert(as);
                }
            }
            else
            {
                scanf("%d",&x);
                for(a=0;a<(1<<m);a++)
                {
                    int as;
                    as=0;
                    for(j=0;j<m;j++)
                    {
                        if(a&(1<<j))as+=maps[x][j];
                        else as-=maps[x][j];
                    }
                    i1=se[a].find(as);
                    se[a].erase(i1);
                }
            }
            int ans=0;
            for(a=0;a<(1<<m);a++)
            {
                i1=se[a].begin();
                i2=se[a].end();
                i2--;
                ans=max(ans,(*i2)-(*i1));
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/rowanhaoa/article/details/9970399