贪心+线性基--bzoj2460: [BeiJing2011]元素

传送门

突然发现自己忘了线性基怎么写了···于是复习一下

裸的线性基,要权值最大就按权值排序然后一个一个加。

线性基: a x a_x ax的二进制第 x x x位为1,构造方法就是从高位到低位扫描每个数 p p p,如果 x x x这一位是 1 1 1,并且 a x = 0 a_x=0 ax=0,那么让 a x = p a_x=p ax=p,跳出循环,否则就让 p p p^ = a x =a_x =ax

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define maxn 1005
#define LL long long
using namespace std;

template<class T>inline void rd(T &x){
	x=0; short f=1; char c=getchar();
	while(c<'0' || c>'9') f=c=='-'?0:1,c=getchar();
	while(c<='9' && c>='0') x=x*10+(c^'0'),c=getchar();
	x=f?x:-x;
}

int n,ans;
LL a[65];
struct Stone{
	LL num; int val;
	bool operator <(const Stone &x) const{
		return val>x.val;
	}
}p[maxn];

int main(){
	rd(n);
	for(int i=1;i<=n;i++){
		rd(p[i].num); rd(p[i].val);
	}
	sort(p+1,p+n+1);
	for(int i=1;i<=n;i++)
		for(int j=60;j>=0;j--)
			if((1LL<<j)&p[i].num){
				if(!a[j]) {a[j]=p[i].num,ans+=p[i].val;break;}
				else p[i].num^=a[j];
			}
	printf("%d\n",ans);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值