BZOJ-2460: [BeiJing2011]元素(线性基任选元素异或和不为0)

本文通过一个具体的题目实例,介绍了如何运用线性基的性质进行高效问题求解。通过对输入数据进行特殊处理和排序,结合线性基的插入操作,实现了快速找到满足条件的元素组合,并计算出最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

解题心得:就考察了一个线性基的性质,就是线性基中任选元素异或和不为 0 0 0。就利用这个性质,按照 b b b降序排序,然后在插入过程中,只要能插入进去答案就可以直接加上 b b b



#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1010;
ll n, cxx[maxn];

struct Node {
    ll a, b;

    bool operator < (const Node &x) const {
        return b > x.b;
    }
}node[maxn];

bool insert(ll x) {
    for(int i=62;i>=0;i--) {
        if(x&(1ll<<i)) {
            if(cxx[i] == 0) {
                cxx[i] = x;
                break;
            } else
                x ^= cxx[i];
        }
    }
    return x > 0;
}

int main() {
//    freopen("1.in.txt", "r", stdin);
    scanf("%lld", &n);
    for(int i=1;i<=n;i++) {
        scanf("%lld%lld", &node[i].a, &node[i].b);
    }
    sort(node+1, node+1+n);

    ll ans = 0;
    for(int i=1;i<=n;i++) {
        if(insert(node[i].a)) {
            ans += node[i].b;
        }
    }
    printf("%lld\n", ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值