【bzoj 2460】元素

傳送門~

解題思路

關於線性基請參照這位大佬的博客:http://www.cnblogs.com/vb4896/p/6149022.html
求出線性基順便貪一下。
代碼:

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<iostream>
#include<cstdlib>
using namespace std;
struct ldx{
    int x;
    long long num;
    bool operator < (const ldx p) const {
        return x>p.x;
    } 
}s[1005];
long long a[65];
int n,ans;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    scanf("%lld%d",&s[i].num,&s[i].x);
    sort(s+1,s+n+1);
    for(int i=1;i<=n;i++){
        for(int j=63;j>=0;j--)
        if(s[i].num>>j&1){
            if(!a[j]) {a[j]=s[i].num;break;}
            else s[i].num^=a[j];
        }
        if(s[i].num!=0) ans+=s[i].x;
    }
    printf("%d",ans);
    return 0;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章