Fork me on GitHub

[BJWC2011]元素

[BJWC2011]元素

简化题意 $:$

给定一个序列,让你从中选出一个子集,使得该子集的任意一个非空子集的异或和都不为 $0$,且最大化选出的子集的代数和.

线性基裸题对叭.

考虑贪心思路,首先我们肯定优先选择权重最大的.

于是我们就从大往小向线性基中插入,如果能成功插入,就统计 $ans$.
否则,无视.

为什么呢?

还记得之前说过,向线性基中插入有两种结果 $:$

$1.$ 被异或为 $0$
$2.$ 在某一位成功插入退出.

这分别有什么意义呢$?$

第一种情况其实就是说,现在的线性基已经可以通过异或得到该数字了,换句话说,就是再加入这个数字,就一定会出现一个非空子集使得它的异或和为 $0$.

第二种情况呢 $?$.其实就是现在的线性基中还缺少这个数的某一位,也就是说加入这个数字一定不会出现一个非空子集使得它的异或和为 $0$.

人类的本质是贪心

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)
#define per(i,a,b) for (int i = (a) ; i >= (b) ; -- i)
#define pii pair < int , int >
#define one first
#define two second
#define rint read<int>
#define int long long
#define pb push_back
#define db double
#define ull unsigned long long
#define lowbit(x) ( x & ( - x ) )

using std::queue ;
using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;
using std::swap ;
using std::sort ;
using std::unique ;
using std::greater ;

template < class T >
inline T read () {
T x = 0 , f = 1 ; char ch = getchar () ;
while ( ch < '0' || ch > '9' ) {
if ( ch == '-' ) f = - 1 ;
ch = getchar () ;
}
while ( ch >= '0' && ch <= '9' ) {
x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
ch = getchar () ;
}
return f * x ;
}

class LinearBasis {
private : int base[65] ;
public :
inline void clear () { MEM ( base , 0 ) ; return ; }
public :
inline bool insert (int x) {
for (int i = 62 ; ~ i ; -- i)
if ( x & ( 1ll << i ) ) {
if ( base[i] ) x ^= base[i] ;
else { base[i] = x ; return true ; }
}
return false ;
}
} B ;

const int N = 1e3 + 100 ;

int n , ans ; pii v[N] ;

inline bool cmp (pii a , pii b) { return a.two > b.two ; }

signed main (int argc , char * argv[]) {
n = rint () ;
rep ( i , 1 , n ) v[i].one = rint () , v[i].two = rint () ;
sort ( v + 1 , v + n + 1 , cmp ) ;
rep ( i , 1 , n ) if ( B.insert ( v[i].one ) ) ans += v[i].two ;
printf ("%lld\n" , ans ) ;
system ("pause") ; return 0 ;
}