[BJWC2011]元素

题目

传送门

题解

线性基入门题 然而我还是看了题解

考虑用 \(\text{Number}_i\) 做线性基,当一个 \(\text{Number}_i\) 可以被放进线性基中时,就表示它与前 \(1\sim i-1\) 异或都不会变成 \(0\),那么我们就可以加上它的贡献。

但是,我们的目标是让答案最大,而由于线性基的特性,其大小是固定的,那么不同的放入顺序会使得线性基不同,即放入前面的某些数之后后面一些数放不进去了。

贪心地,我们先把贡献最大的那些数想办法放进去就好了。

#include<bits/stdc++.h>
using namespace std;

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void qread(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
inline int qread(){
//just for int(When the code defines int long long, the function is just for long long)
    int x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
#undef cg
// template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=1000;
const int MAXLOG=60;

int n;

int q[MAXN+5];

LL id[MAXN+5],mag[MAXN+5],ans;

LL f[MAXLOG+5],w[MAXLOG+5];

inline int high_pos(LL num){
    int ret=-1;
    while(num>0)num>>=1,++ret;
    return ret;
}

inline bool cmp(const int i,const int j){
    return mag[i]>mag[j];
}

signed main(){
    cin>>n;
    rep(i,1,n)cin>>id[i]>>mag[i],q[i]=i;
    sort(q+1,q+n+1,cmp);
    int t;
    rep(i,1,n){
        while(f[t=high_pos(id[q[i]])])id[q[i]]^=f[t];
        if(id[q[i]])f[t]=id[q[i]],ans+=mag[q[i]];
    }
    cout<<ans<<endl;
    return 0;
}
posted @ 2020-05-11 16:12  Arextre  阅读(122)  评论(0编辑  收藏  举报