<span>Luogu P4570 [BJWC2011]元素</span>

题目很显然就是要求序号的线性基。我们希望线性基里的权值最大,就按权值从大到小插入就行了。
为什么是对的呢?插入线性基的一个矿石只会和另一些矿石在一个位置上冲突,而那些矿石也只能插入这一位,同时它们价值不如当前的矿石,所以这是最优的。

#include <cstdio>
#include <algorithm>
using namespace std;

#define R register
#define LL long long

const int MAXN=1e3+10;
const int MB=62;

int n;
struct Node { LL x; int y; } a[MAXN];
inline bool cmp(Node x,Node y) { return x.y>y.y; }

LL p[MB+1];
inline int  ins(LL x) {
	for(R int i=MB;i>=0;i--)
		if(x&(1LL<<i)) {
			if(!p[i]) { p[i]=x; return 1;}
			else x^=p[i];
		}
	return 0;
}

int main() {
	scanf("%d",&n);
	for(R int i=1;i<=n;i++)
		scanf("%lld%d",&a[i].x,&a[i].y);
	sort(a+1,a+1+n,cmp);	
	int ans=0;
	for(R int i=1;i<=n;i++)
		if(ins(a[i].x)) ans+=a[i].y;
	printf("%d\n",ans);
	return 0;
}
全部评论

相关推荐

牛客517626884号:嵌入式真难啊今年,我电赛国二都成了路边野狗了
点赞 评论 收藏
分享
秋盈丶:后续:我在宿舍群里和大学同学分享了这事儿,我好兄弟气不过把他挂到某脉上了,10w+阅读量几百条评论,直接干成精品贴子,爽
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务