[BJWC2011]元素

嘟嘟嘟


题中说选的数的编号亦或和不能为0,也就是在这个集合中,不能用不同的选取方案亦或出相同的值。由此联想到线性基的一个性质是,每一个数都能由线性基中特定的一些数亦或得到。
所以我们就是要求出这些数的线性基,并且满足所选的数的魔力值的和最大。
本来以为是dp,结果按魔力值排个序贪心就过了。


证明在网上找了半天,终于找到了一片很不错的题解:传送门.。
利用的性质就是如果原数组固定,线性基的大小就是固定的。所以我们贪心的插入魔力值大的数。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
const int maxN = 63;
inline ll read()
{
    ll ans = 0;
    char ch = getchar(), last = ' ';
    while(!isdigit(ch)) {last = ch; ch = getchar();}
    while(isdigit(ch)) {ans = (ans << 1) + (ans << 3) + ch - '0'; ch = getchar();}
    if(last == '-') ans = -ans;
    return ans;
}
inline void write(ll x)
{
    if(x < 0) x = -x, putchar('-');
    if(x >= 10) write(x / 10);
    putchar(x % 10 + '0');
}

int n;
struct Node
{
    ll id; int num;
    bool operator < (const Node& oth)const
    {
        return num > oth.num;
    }
}t[maxn];

ll p[maxN];
In bool insert(ll x)
{
    for(int i = maxN; i >= 0; --i) if((x >> i) & 1)
    {
        if(p[i]) x ^= p[i];
        else return p[i] = x;
    }
    return 0;
}

int main()
{
    n = read();
    for(int i = 1; i <= n; ++i) t[i].id = read(), t[i].num = read();
    sort(t + 1, t + n + 1);
    int ans = 0;
    for(int i = 1; i <= n; ++i) if(insert(t[i].id)) ans += t[i].num;
    write(ans), enter;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/mrclr/p/10255659.html
今日推荐