標籤:tor += 最大值 mat inline cti turn line bool
[BJWC2011]元素題目大意:
\(n(n\le1000)\)個物品,每個物品有兩個屬性:序號\(a_i(a_i\le10^{18})\)和權值\(b_i(b_i\le10000)\)。現在從中選取若干個物品,使得序號異或和不為\(0\),求權值和最大值。
思路:
按照權值從大到小排序貪心地構造線性基。
原始碼:
#include<cstdio>#include<cctype>#include<algorithm>#include<functional>typedef long long int64;inline int64 getint() { register char ch; while(!isdigit(ch=getchar())); register int64 x=ch^'0'; while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0'); return x;}const int N=1000,B=60;struct Node { int64 id; int w; bool operator > (const Node &rhs) const { return w>rhs.w; }};Node a[N],b[B];int main() { const int n=getint(); for(register int i=0;i<n;i++) { a[i].id=getint(); a[i].w=getint(); } std::sort(&a[0],&a[n],std::greater<Node>()); for(register int i=0;i<n;i++) { for(register int j=B-1;j>=0;j--) { if((a[i].id>>j)&1) { if(b[j].id==0) { b[j]=a[i]; break; } a[i].id^=b[j].id; } } } int ans=0; for(register int i=0;i<B;i++) { ans+=b[i].w; } printf("%d\n",ans); return 0;}
[BJWC2011]元素