洛谷P4570 [BJWC2011]元素(线性基)

传送门

 

 

不知道线性基是什么东西的可以看看蒟蒻的总结

考虑贪心

将所有的矿石按价值从大到小排序

如果一块矿石不会和之前的编号异或为0就加入

这个只要判一下它能不能加进线性基里就可以了

据说这个贪心的证明得用拟阵……反正我是不会

 1 //minamoto
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #define ll long long
 6 using namespace std;
 7 #define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
 8 char buf[1<<21],*p1=buf,*p2=buf;
 9 inline ll read(){
10     #define num ch-'0'
11     char ch;bool flag=0;ll res;
12     while((ch=getc())>'9'||ch<'0')
13     (ch=='-')&&(flag=true);
14     for(res=num;(ch=getc())<='9'&&ch>='0';res=res*10+num);
15     (flag)&&(res=-res);
16     #undef num
17     return res;
18 }
19 const int N=1005;
20 struct node{
21     ll num;int p;
22     node(){}
23     node(ll num,int p):num(num),p(p){}
24     inline bool operator <(const node &b)const
25     {return p>b.p;}
26 }a[N];
27 ll b[64],num;int n,ans,p;
28 inline bool insert(ll x){
29     for(int i=63;i>=0;--i)
30     if(x>>i&1){
31         if(!b[i]) return b[i]=x,true;
32         x^=b[i];
33     }
34     return false;
35 }
36 int main(){
37 //    freopen("testdata.in","r",stdin);
38     n=read();
39     for(int i=1;i<=n;++i) num=read(),p=read(),a[i]=node(num,p);
40     sort(a+1,a+1+n);
41     for(int i=1;i<=n;++i)
42     if(insert(a[i].num)) ans+=a[i].p;
43     printf("%d\n",ans);
44     return 0;
45 }

 

posted @ 2018-09-27 21:49  bztMinamoto  阅读(137)  评论(0编辑  收藏  举报
Live2D