Loading

BZOJ 2460:元素(贪心+线性基)

题目链接

题意

中文题意

思路

线性基学习

题目要求选价值最大的并且这些数异或后不为0,可以考虑线性基的性质:线性基的任意一个非空集合XOR之和不会为0。那么就可以贪心地对价值从大到小排序,加入线性基的数就加上它的价值,最终线性基里面的元素的价值就是最终答案。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int N = 1e3 + 11;
const int M = 65;
struct Node {
    LL id; int w;
    bool operator < (const Node &rhs) const {
        return w > rhs.w;
    }
} a[N];
LL p[M];

int solve(int n) {
    int ans = 0;
    sort(a, a + n);
    for(int i = 0; i < n; i++) {
        for(int j = 63; j >= 0; j--) {
            if(((a[i].id >> j) & 1) == 0) continue;
            if(!p[j]) { p[j] = a[i].id, ans += a[i].w; break; }
            a[i].id ^= p[j];
        }
    } return ans;
}

int main() {
    int n; scanf("%d", &n);
    for(int i = 0; i < n; i++) scanf("%lld%d", &a[i].id, &a[i].w);
    printf("%d", solve(n));
}
posted @ 2017-10-09 13:16  Shadowdsp  阅读(144)  评论(0编辑  收藏  举报