题意:给定n个物品,每个物品有属性x和价值y,要求从中选出一些使得价值和最大并且其中没有属性xor和为0的非空子集
n<=1000,x<=1e18,y<=1e4
思路:没有xor和为0的非空子集本来就是线性基的定义
拟阵,直接按价值排序之后贪心插入并维护线性基
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 typedef unsigned int uint; 5 typedef unsigned long long ull; 6 typedef long double ld; 7 typedef pair<int,int> PII; 8 typedef pair<ll,ll> Pll; 9 typedef vector<int> VI; 10 typedef vector<PII> VII; 11 typedef pair<ll,ll>P; 12 #define N 100010 13 #define M 1000000 14 #define INF 1e9 15 #define fi first 16 #define se second 17 #define MP make_pair 18 #define pb push_back 19 #define pi acos(-1) 20 #define mem(a,b) memset(a,b,sizeof(a)) 21 #define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++) 22 #define per(i,a,b) for(int i=(int)a;i>=(int)b;i--) 23 #define lowbit(x) x&(-x) 24 #define Rand (rand()*(1<<16)+rand()) 25 #define id(x) ((x)<=B?(x):m-n/(x)+1) 26 #define ls p<<1 27 #define rs p<<1|1 28 #define fors(i) for(auto i:e[x]) if(i!=p) 29 30 const int MOD=1e9+7,inv2=(MOD+1)/2; 31 double eps=1e-6; 32 int dx[4]={-1,1,0,0}; 33 int dy[4]={0,0,-1,1}; 34 35 int read() 36 { 37 int v=0,f=1; 38 char c=getchar(); 39 while(c<48||57<c) {if(c=='-') f=-1; c=getchar();} 40 while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar(); 41 return v*f; 42 } 43 44 ll readll() 45 { 46 ll v=0,f=1; 47 char c=getchar(); 48 while(c<48||57<c) {if(c=='-') f=-1; c=getchar();} 49 while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar(); 50 return v*f; 51 } 52 53 struct node 54 { 55 ll x; 56 int y; 57 }a[N]; 58 59 bool cmp(node a,node b) 60 { 61 return a.y>b.y; 62 } 63 64 struct base 65 { 66 ll d[65],p[65]; 67 int cnt; 68 69 base() 70 { 71 mem(d,0); 72 mem(p,0); 73 cnt=0; 74 } 75 76 bool insert(ll x) 77 { 78 //if(x==0) return 0; 79 per(i,62,0) 80 if(x>>i&1) 81 { 82 if(!d[i]) 83 { 84 d[i]=x; 85 break; 86 } 87 x^=d[i]; 88 } 89 return x>0; 90 } 91 }T; 92 93 int main() 94 { 95 int n=read(); 96 rep(i,1,n) scanf("%lld%d",&a[i].x,&a[i].y); 97 sort(a+1,a+n+1,cmp); 98 base T; 99 int ans=0; 100 rep(i,1,n) if(T.insert(a[i].x)) ans+=a[i].y; 101 printf("%d ",ans); 102 return 0; 103 }