Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P4570 [BJWC2011]元素

P4570 [BJWC2011]元素

任意子集异或和不为 $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
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
template <class Type>
void read(Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
flag && (x = ~x + 1);
}
template <class Type>
void write(Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar(x % 10 + '0');
}
const int N = 1e3 + 10, M = 60;
struct Node
{
LL t;
int k;
bool operator < (const Node &_) const
{
return k > _.k;
}
} w[N];
int n, ans;
LL bs[M];
void insert (LL x, int k)
{
for (int i = M - 1; ~i; i--)
if (x >> i & 1)
{
if (!bs[i])
return bs[i] = x, ans += k, void();
x ^= bs[i];
}
}
int main ()
{
read(n);
for (int i = 0; i < n; i++)
read(w[i].t), read(w[i].k);
sort(w, w + n);
for (int i = 0; i < n; i++)
insert(w[i].t, w[i].k);
write(ans);
return 0;
}