zoukankan      html  css  js  c++  java
  • BZOJ 2460:元素(贪心+线性基)

    题目链接

    题意

    中文题意

    思路

    线性基学习

    题目要求选价值最大的并且这些数异或后不为0,可以考虑线性基的性质:线性基的任意一个非空集合XOR之和不会为0。那么就可以贪心地对价值从大到小排序,加入线性基的数就加上它的价值,最终线性基里面的元素的价值就是最终答案。

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> pii;
    const int INF = 0x3f3f3f3f;
    const int N = 1e3 + 11;
    const int M = 65;
    struct Node {
        LL id; int w;
        bool operator < (const Node &rhs) const {
            return w > rhs.w;
        }
    } a[N];
    LL p[M];
    
    int solve(int n) {
        int ans = 0;
        sort(a, a + n);
        for(int i = 0; i < n; i++) {
            for(int j = 63; j >= 0; j--) {
                if(((a[i].id >> j) & 1) == 0) continue;
                if(!p[j]) { p[j] = a[i].id, ans += a[i].w; break; }
                a[i].id ^= p[j];
            }
        } return ans;
    }
    
    int main() {
        int n; scanf("%d", &n);
        for(int i = 0; i < n; i++) scanf("%lld%d", &a[i].id, &a[i].w);
        printf("%d", solve(n));
    }
    
  • 相关阅读:
    表达式的计算
    树、森林、与二叉树的转换
    线索二叉树
    表达式树
    js正则表达式处理表单
    kmp匹配算法
    SQL练习题
    ajax添加header信息
    mvc中webapi添加后没法访问 解决办法
    mysql修改表引擎Engine
  • 原文地址:https://www.cnblogs.com/fightfordream/p/7640665.html
Copyright © 2011-2022 走看看