2460: [BeiJing2011]元素

2460: [BeiJing2011]元素

链接

分析:

  贪心的想:首先按权值排序,然后从大到小依次放,能放则放。然后用线性基维护是否合法。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
#define pa pair<int,LL>
using namespace std;
typedef long long LL;

inline LL read() {
    LL x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
    for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
}

const int N = 2005;
pa A[N];
LL b[N];

bool Insert(LL x) {
    for (int i = 60; ~i; --i) 
        if ((x >> i) & 1) {
            if (b[i]) x ^= b[i];
            else {
                b[i] = x; 
                return 1;
            }
        }
    return 0;
}

int main() {
    int n = read();
    for (int i = 1; i <= n; ++i) 
        A[i].second = read(), A[i].first = read();
    sort(A + 1, A + n + 1);
    LL Ans = 0;
    for (int i = n; i >= 1; --i) 
        if (Insert(A[i].second)) Ans += A[i].first;
    cout << Ans;
    return 0;
}
原文地址:https://www.cnblogs.com/mjtcn/p/10375806.html