P4570 [BJWC2011]元素

题意:有n个东西,每个东西能选当且仅当它的元素序号不能通过之前选过的东西的元素序号异或得到,在此前提下,求魔力的最大值。

思路:线性基定理可得,线性基中元素个数确定。优先插入魔力值大的即可。由于将一个原来插入不进线性基的元素插入到线性基里面,只需要删去线性基里面的一个特定的元素就好了,因此贪心的插入可得最大值

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<stack>
#include<cstdlib>
#include<queue>
#include<set>
#include<string.h>
#include<vector>
#include<deque>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define eps 1e-4
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
typedef long long LL;
typedef long long ll;
const int maxn = 2e3 + 5;
const int mod = 998244853;

struct node{
    LL x,y;
}a[maxn];
bool cmp(node a,node b) {
    return a.y > b.y;
}
LL d[65];
bool add(LL x) {
    for(int i = 60; i >= 0 ; i--) {
        if(x & (1LL << i)) {
            if(d[i]) {
                x ^= d[i];
            }
            else {
                d[i] = x;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i = 0; i < n; i++) scanf("%lld %lld",&a[i].x,&a[i].y);
    sort(a, a + n, cmp);
    LL ans = 0;
    for(int i = 0; i < n; i++) {
        if(add(a[i].x))
            ans += a[i].y;
    }
    printf("%lld\n",ans);
}

 

posted @ 2019-08-26 22:05  千摆渡Qbd  阅读(126)  评论(0)    收藏  举报