2460: [BeiJing2011]元素
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 788 Solved: 421
[ Submit][ Status][ Discuss]
Description
相传,在远古时期,位于西方大陆的 Magic Land 上,人们已经掌握了用魔
法矿石炼制法杖的技术。那时人们就认识到,一个法杖的法力取决于使用的矿石。
一般地,矿石越多则法力越强,但物极必反:有时,人们为了获取更强的法力而
使用了很多矿石,却在炼制过程中发现魔法矿石全部消失了,从而无法炼制
出法杖,这个现象被称为“魔法抵消” 。特别地,如果在炼制过程中使用超过
一块同一种矿石,那么一定会发生“魔法抵消”。
后来,随着人们认知水平的提高,这个现象得到了很好的解释。经过了大量
的实验后,著名法师 Dmitri 发现:如果给现在发现的每一种矿石进行合理的编
号(编号为正整数,称为该矿石的元素序号),那么,一个矿石组合会产生“魔
法抵消”当且仅当存在一个非空子集,那些矿石的元素序号按位异或起来
为零。 (如果你不清楚什么是异或,请参见下一页的名词解释。 )例如,使用两
个同样的矿石必将发生“魔法抵消”,因为这两种矿石的元素序号相同,异或起
来为零。
并且人们有了测定魔力的有效途径,已经知道了:合成出来的法杖的魔力
等于每一种矿石的法力之和。人们已经测定了现今发现的所有矿石的法力值,
并且通过实验推算出每一种矿石的元素序号。
现在,给定你以上的矿石信息,请你来计算一下当时可以炼制出的法杖最多
有多大的魔力。
Input
第一行包含一个正整数N,表示矿石的种类数。
接下来 N行,每行两个正整数Numberi 和 Magici,表示这种矿石的元素序号
和魔力值。
Output
仅包一行,一个整数:最大的魔力值
Sample Input
1 10
2 20
3 30
Sample Output
HINT
由于有“魔法抵消”这一事实,每一种矿石最多使用一块。
如果使用全部三种矿石,由于三者的元素序号异或起来:1 xor 2 xor 3 = 0 ,
则会发生魔法抵消,得不到法杖。
可以发现,最佳方案是选择后两种矿石,法力为 20+30=50。
对于全部的数据:N ≤ 1000,Numberi ≤ 10^18
,Magici ≤ 10^4
。
Source
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int N = 60;
const int maxn = 1010;
typedef long long LL;
struct data{
LL num; int w; data(){}
data(LL num,int w): num(num),w(w){}
bool operator < (const data &b) const {return w > b.w;}
}D[maxn];
int n,Ans;
LL b[N];
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n;
for (int i = 1; i <= n; i++)
{
int w; LL num; scanf("%lld%d",&num,&w);
D[i] = data(num,w);
}
sort(D + 1,D + n + 1);
for (int i = 1; i <= n; i++)
{
LL now = D[i].num;
for (LL j = 0; j < N; j++)
if (now&(1LL<<j))
{
if (b[j]) now ^= b[j];
else {b[j] = now; Ans += D[i].w; break;}
}
}
cout << Ans;
return 0;
}