luoguP4570 [BJWC2011]元素(线性基)

传送:https://www.luogu.org/problem/P4570

题意:给定一些物品,每种物品有两个属性,序号和魔力值,现在在这些物品中选出几个,要求选中的物品序号异或起来不能是0并且魔力值加起来最大,求出这个最大值。

分析:

考虑线性基的一个性质:同一组元素的线性基的数量是相同的。

那么贪心考虑答案。如果当前物品序号能够被插入,那么对于答案是有贡献的。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=1010;
 5 struct node{
 6     ll x,y;
 7 }a[maxn]; 
 8 bool cmp(node p,node q){return p.y>q.y;}
 9 struct Linear_basis{
10     ll b[65],tot;
11     bool insert(ll x){
12         for (int i=60;i>=0;i--){
13             if (x&(1ll<<i)){
14                 if (!b[i]){
15                     b[i]=x; break;
16                 } 
17                 x^=b[i];
18             }
19         }
20         return x>0;  //是否可插入 true:可插入,false:不可插入 
21     }
22 }LB;
23 int main(){
24     int n;scanf("%d",&n); ll x;
25     for (int i=1;i<=n;i++) scanf("%lld%lld",&a[i].x,&a[i].y);
26     sort(a+1,a+1+n,cmp);
27     ll ans=0;
28     for (int i=1;i<=n;i++){
29         bool f=LB.insert(a[i].x);
30         if (f) ans+=a[i].y;
31     }
32     printf("%lld\n",ans);
33     return 0; 
34 }

 

猜你喜欢

转载自www.cnblogs.com/changer-qyz/p/11333756.html
今日推荐