码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 2460: [BeiJing2011]元素 贪心,线性基

时间:2017-08-29 20:40:06      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:line   const   node   bin   for   logs   pac   添加   lld   

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2460

解法:从大到小排序,依次贪心的添加到当前集合就可以了,需要动态维护线性基。用拟阵证明,线性基性质,线性基中任意子集异或和不为0,所以从大到小加入就好。

 

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
struct node{
    LL a, b;
    node(){}
    bool operator<(const node &rhs) const{
        return b>rhs.b;
    }
}a[1005];
int n;
LL bin[65];
LL p[65];
int main()
{
    bin[0] = 1;
    for(int i=1; i<=63; i++) bin[i] = bin[i-1]<<1;
    scanf("%d", &n);
    for(int i=1; i<=n; i++){
        scanf("%lld %lld", &a[i].a, &a[i].b);
    }
    LL ans = 0;
    sort(a+1, a+n+1);
    for(int i=1; i<=n; i++){
        for(int j=63; j>=0; j--){
            if(a[i].a&bin[j]){
                if(!p[j]){
                    p[j] = i;
                    break;
                }
                else{
                    a[i].a ^= a[p[j]].a;
                }
            }
        }
        if(a[i].a) ans += a[i].b;
    }
    printf("%lld\n", ans);
    return 0;
}

 

BZOJ 2460: [BeiJing2011]元素 贪心,线性基

标签:line   const   node   bin   for   logs   pac   添加   lld   

原文地址:http://www.cnblogs.com/spfa/p/7450008.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!