BeiJing2011 元素

本文介绍了一种结合贪心策略与线性基数据结构的算法解决方案,用于处理特定类型的数学问题。通过将矿石按贡献排序,并利用线性基进行查找与更新,实现最优解的求解。

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

题目描述

题解:

贪心+线性基。

先按贡献从小到大排序,然后对于每个矿石扔到线性基中查找。

找不到就加上贡献,推进线性基。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1050;
template<typename T>
inline void read(T&x)
{
    T f = 1,c = 0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){c=c*10+ch-'0';ch=getchar();}
    x = f*c;
}
int n;
struct Pair
{
    ll x;
    int y;
}p[N];
bool cmp(Pair a,Pair b){return a.y>b.y;}
struct lb
{
    ll c[70];
    bool find(ll k)
    {
        for(int i=63;i>=0;i--)if(k&(1ll<<i))
        {
            if(c[i])k^=c[i];
            else return 1;
        }
        return k!=0;
    }
    void insert(ll k)
    {
        for(int i=63;i>=0;i--)if(k&(1ll<<i))
        {
            if(c[i])k^=c[i];
            else
            {
                c[i]=k;
                break;
            }
        }
    }
}tr;
int main()
{
    read(n);
    for(int i=1;i<=n;i++)
        read(p[i].x),read(p[i].y);
    sort(p+1,p+1+n,cmp);
    ll ans = 0;
    for(int i=1;i<=n;i++)
    {
        if(tr.find(p[i].x))
        {
            tr.insert(p[i].x);
            ans+=p[i].y;
        }
    }
    printf("%lld\n",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/LiGuanlin1124/p/10652019.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值