首页 > 其他 > 详细

[BJWC2011]元素

时间:2018-10-05 20:06:17      阅读:161      评论:0      收藏:0      [点我收藏+]

[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;
}

[BJWC2011]元素

原文:https://www.cnblogs.com/skylee03/p/9745663.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!