[BJWC2011]元素

[BJWC2011]元素

题目大意:

\(n(n\le1000)\)个物品,每个物品有两个属性:序号\(a_i(a_i\le10^{18})\)和权值\(b_i(b_i\le10000)\)。现在从中选取若干个物品,使得序号异或和不为\(0\),求权值和最大值。

思路:

按照权值从大到小排序贪心地构造线性基。

源代码:

#include<cstdio>
#include<cctype>
#include<algorithm>
#include<functional>
typedef long long int64;
inline int64 getint() {
    register char ch;
    while(!isdigit(ch=getchar()));
    register int64 x=ch^'0';
    while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
    return x;
}
const int N=1000,B=60;
struct Node {
    int64 id;
    int w;
    bool operator > (const Node &rhs) const {
        return w>rhs.w;
    }
};
Node a[N],b[B];
int main() {
    const int n=getint();
    for(register int i=0;i<n;i++) {
        a[i].id=getint();
        a[i].w=getint();
    }
    std::sort(&a[0],&a[n],std::greater<Node>());
    for(register int i=0;i<n;i++) {
        for(register int j=B-1;j>=0;j--) {
            if((a[i].id>>j)&1) {
                if(b[j].id==0) {
                    b[j]=a[i];
                    break;
                }
                a[i].id^=b[j].id;
            }
        }
    }
    int ans=0;
    for(register int i=0;i<B;i++) {
        ans+=b[i].w;
    }
    printf("%d\n",ans);
    return 0;
}

转载于:https://www.cnblogs.com/skylee03/p/9745663.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值