zoukankan      html  css  js  c++  java
  • BZOJ 2460: [BeiJing2011]元素 贪心,线性基

    题目链接: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
    ", ans);
        return 0;
    }
    
  • 相关阅读:
    结对第一次—原型设计(文献摘要热词统计)
    第一次作业-准备篇
    Alpha冲刺Day9
    Alpha冲刺Day8
    Alpha冲刺Day7
    Alpha冲刺Day6
    Alpha冲刺Day5
    Alpha冲刺Day4
    Alpha冲刺Day3
    Alpha冲刺Day2
  • 原文地址:https://www.cnblogs.com/spfa/p/7450008.html
Copyright © 2011-2022 走看看