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));
    }
    
  • 相关阅读:
    android作业10.21
    安卓10.7作业
    安卓9.30
    9.23作业
    9.17安卓作业
    6.12作业
    5.29作业
    5.28上机作业
    leetcode 219
    策略模式
  • 原文地址:https://www.cnblogs.com/fightfordream/p/7640665.html
Copyright © 2011-2022 走看看