题目链接
我们给出N个矿石,有num表示其序号,还有Magic表示其价值,我们要一些矿石使得这些矿石的异或和不为0并且还要求其价值最大。
那么,我们在这里需要用到线性基的性质,就是假如与前面所有的p[i]都异或为0的话,表示它肯定就是放不进来的了,然后呢,我们要怎么取呢,这就是个贪心的过程了。假如,我们有a[1] …… a[N]这么多的价值,我们对a进行降序排列,然后呢,如果我们取a[1],就不能取a[2]、a[3]、……、a[i],那么,换一下,我们不取a[i]是不是就可以取a[1]了呀。所以越在前面的就越是要取。
接下来考虑a[j]
如果a[j]可以被a[1]到a[j-1]中的数线性表示,那么a[j]肯定不优,不加入答案
此时即a[j]无法插入到线性基里面
如果a[j]不能被a[1]到a[j-1]中的数线性表示,即a[j]可以被插入线性基里
那么如果a[j]不在答案,那么a[j]必定可以被a[1]到a[j-1]与a[j+1]到a[n]中的数线性表示
其中a[j+1]到a[n]中至少存在一个
那么我们用a[j]替换这个数,可以使得所有数的异或不为0
又a[j]的价值一定比a[j+1]到a[n]中的任意一个大
因此a[j]如果能够插入线性基,就一定会在答案里
综上我们只需要按照value从大到小排序后依次插入线性基,把能够插入的累加到ans里即可
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define efs 1e-6
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MAX_3(a, b, c) max(max(a, b), c)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e3 + 75;
struct node
{
ll num, mag;
}a[maxN];
bool cmp(node e1, node e2) { return e1.mag > e2.mag; }
int N;
ll p[maxN], ans;
inline void LB(int id)
{
for(int i=61; i>=0; i--)
{
if( !( a[id].num >> (ll)i ) ) continue;
if(!p[i])
{
p[i] = a[id].num;
ans += a[id].mag;
return;
}
a[id].num ^= p[i];
}
}
inline void init()
{
ans = 0;
memset(p, 0, sizeof(p));
}
int main()
{
scanf("%d", &N);
init();
for(int i=1; i<=N; i++)
{
scanf("%lld%lld", &a[i].num, &a[i].mag);
}
sort(a + 1, a + N + 1, cmp);
for(int i=1; i<=N; i++) LB(i);
printf("%lld\n", ans);
return 0;
}