hdu4666Hyperspace

http://acm.hdu.edu.cn/showproblem.php?pid=4666

先看一个求曼哈顿的帖子http://www.cnblogs.com/lmnx/articles/2479747.html

然后用mulityset进行维护下就可以了

 1 #include <iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 #include<algorithm>

 5 #include<stdlib.h>

 6 #include<set>

 7 using namespace std;

 8 #define N 60010

 9 int w[N][10];

10 int main()

11 {

12     int i,j,q,g,k;

13     while(cin>>q>>k)

14     {

15         multiset<int>p[40];

16         multiset<int>::iterator it;

17         for(g = 1 ; g <= q ; g++)

18         {

19             int a;

20             scanf("%d",&a);

21             if(a==0)

22             {

23                 for(i =0; i < k ; i++)

24                 scanf("%d",&w[g][i]);

25                 for(i = 0 ; i < 1<<k ; i++)

26                 {

27                     int temp = 0;

28                     for(j =0 ; j < k ; j++)

29                     {

30                         if(i&(1<<j))

31                         temp+=w[g][j];

32                         else

33                         temp-=w[g][j];

34                     }

35                     p[i].insert(temp);

36                 }

37             }

38             else

39             {

40                 int x;

41                 scanf("%d",&x);

42                 for(i = 0 ; i < 1<<k ; i++)

43                 {

44                     int temp=0;

45                     for(j = 0 ; j < k ; j++)

46                     {

47                         if(i&(1<<j))

48                         temp+=w[x][j];

49                         else

50                         temp-=w[x][j];

51                     }

52                     it = p[i].find(temp);

53                     p[i].erase(it);

54                 }

55             }

56             int maxz=0;

57             for(i  =0 ; i < 1<<k ; i++)

58             {

59                 j =(~i)&((1<<k)-1);

60                 int t1,t2;

61                 it = p[i].end();

62                 it--;

63                 t1 = (*it);

64                 it = p[j].end();

65                 it--;

66                 t2 = (*it);

67                 maxz = max(maxz,t1+t2);

68             }

69             cout<<maxz<<endl;

70         }

71     }

72     return 0;

73 }
View Code

 

你可能感兴趣的:(HDU)