BZOJ 2460 元素(贪心+线性基)

显然线性基可以满足题目中给出的条件。关键是如何使得魔力最大。

贪心策略是按魔力排序,将编号依次加入线性基,一个数如果和之前的一些数异或和为0就跳过他。

因为如果要把这个数放进去,那就要把之前的某个数拿出来,而这样交换之后集合能异或出的数是不会变的,和却变小了。

 

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-3
# define MOD 1000000007
# define INF 1000000000
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
    int res=0, flag=0;
    char ch;
    if((ch=getchar())=='-') flag=1;
    else if(ch>='0'&&ch<='9') res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')  res=res*10+(ch-'0');
    return flag?-res:res;
}
void Out(int a) {
    if(a<0) {putchar('-'); a=-a;}
    if(a>=10) Out(a/10);
    putchar(a%10+'0');
}
const int N=1005;
//Code begin...

typedef struct {LL num; int magic;}Node;
Node a[N];
LL p[63];

bool comp(Node a, Node b){return a.magic>b.magic;}
int main ()
{
    int n, ans=0;
    scanf("%d",&n);
    FOR(i,1,n) scanf("%lld%d",&a[i].num,&a[i].magic);
    sort(a+1,a+n+1,comp);
    FOR(i,1,n) {
        for (int j=62; j>=0; --j) {
            if (!(a[i].num>>j)) continue;
            if (!p[j]) {p[j]=a[i].num; ans+=a[i].magic; break;}
            a[i].num^=p[j];
        }
    }
    printf("%d\n",ans);
    return 0;
}
View Code

 

posted @ 2017-02-06 23:21  free-loop  阅读(146)  评论(0编辑  收藏  举报