线性基 最大魔法值 P4570 [BJWC2011]元素

本文介绍了一道关于线性基的算法题,通过贪心策略选择序列元素,实现最优解。使用C++实现,涉及位运算和排序,详细展示了如何通过线性基结构解决特定类型的问题。

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

题目链接:https://www.luogu.org/problem/P4570
题目大意:

在这里插入图片描述
在这里插入图片描述
对于一个序列,线性基的元素个数是一定的,所以只要贪心的选择就可以了。

#include <bits/stdc++.h>
#define LL long long
using namespace std;

struct node
{
    LL Id, x;

}a[1005];
LL place[70]={0};

bool cmp(node &a, node &b)
{
    return a.x>b.x;
}

void Insert(long long x)
{
    for(int i=63;~i;i--) if((x>>i)&1ll)
    {
        if(!place[i]) {place[i]=x;return;}
        else x^=place[i];
    }
}

bool check(long long x)
{
    for(int i=63;~i;i--)
        if((x>>i)&1ll) x^=place[i];

    return x==0;
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%lld%lld",&a[i].Id, &a[i].x);
    }
    sort(a, a+n, cmp);

    LL ans=0;
    for(int i=0;i<n;i++)
    {
        if(!check(a[i].Id))
        {
            ans+=a[i].x;
            Insert(a[i].Id);
        }
    }
    cout<<ans<<endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值