BJWC2011 元素

传送门

线性基有一个重要的性质:线性基中任意一个非空子集的异或和不为0。

这好像就是给这道题准备的!

立即得到做法:按权值从大到小排序,直接插入线性基计算答案即可。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define rep(i,a,n) for(register int i = a;i <= n;i++)
#define per(i,n,a) for(register int i = n;i >= a;i--)
#define enter putchar('\n')
#define pr pair<int,int>
#define mp make_pair
#define fi first
#define sc second
using namespace std;
typedef long long ll;
const int M = 100005;
const int N = 10000005;
 
ll read()
{
   ll ans = 0,op = 1;char ch = getchar();
   while(ch < '0' || ch > '9') {if(ch == '-') op = -1;ch = getchar();}
   while(ch >='0' && ch <= '9') ans = ans * 10 + ch - '0',ch = getchar();
   return ans * op;
}

struct stone
{
   ll num,mag;
   bool operator < (const stone &g) const
   {
      return mag > g.mag;
   }
}s[M];

ll n,ans,p[100];

void insert(ll x,ll v)
{
   per(i,63,0)
   {
      if(!(x >> i)) continue;
      if(!p[i]) {p[i] = x,ans += v;break;}
      x ^= p[i];
   }
}

int main()
{
   n = read();
   rep(i,1,n) s[i].num = read(),s[i].mag = read();
   sort(s+1,s+1+n);
   rep(i,1,n) insert(s[i].num,s[i].mag);
   printf("%lld\n",ans);
   return 0;
}

posted @ 2019-01-01 23:23  CaptainLi  阅读(121)  评论(0编辑  收藏  举报