zoukankan      html  css  js  c++  java
  • [BJWC2011]元素

    [BJWC2011]元素

    简化题意 (:)

    给定一个序列,让你从中选出一个子集,使得该子集的任意一个非空子集的异或和都不为 (0),且最大化选出的子集的代数和.

    线性基裸题对叭.

    考虑贪心思路,首先我们肯定优先选择权重最大的.

    于是我们就从大往小向线性基中插入,如果能成功插入,就统计 (ans).
    否则,无视.

    为什么呢?

    还记得之前说过,向线性基中插入有两种结果 (:)

    (1.) 被异或为 (0)
    (2.) 在某一位成功插入退出.

    这分别有什么意义呢(?)

    第一种情况其实就是说,现在的线性基已经可以通过异或得到该数字了,换句话说,就是再加入这个数字,就一定会出现一个非空子集使得它的异或和为 (0).

    第二种情况呢 (?).其实就是现在的线性基中还缺少这个数的某一位,也就是说加入这个数字一定不会出现一个非空子集使得它的异或和为 (0).

    人类的本质是贪心

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <queue>
    #include <cmath>
    #include <ctime>
    #include <map>
    #include <set>
    #define MEM(x,y) memset ( x , y , sizeof ( x ) )
    #define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)
    #define per(i,a,b) for (int i = (a) ; i >= (b) ; -- i)
    #define pii pair < int , int >
    #define one first
    #define two second
    #define rint read<int>
    #define int long long
    #define pb push_back
    #define db double
    #define ull unsigned long long
    #define lowbit(x) ( x & ( - x ) )
    
    using std::queue ;
    using std::set ;
    using std::pair ;
    using std::max ;
    using std::min ;
    using std::priority_queue ;
    using std::vector ;
    using std::swap ;
    using std::sort ;
    using std::unique ;
    using std::greater ;
    
    template < class T >
        inline T read () {
            T x = 0 , f = 1 ; char ch = getchar () ;
            while ( ch < '0' || ch > '9' ) {
                if ( ch == '-' ) f = - 1 ;
                ch = getchar () ;
            }
           while ( ch >= '0' && ch <= '9' ) {
                x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
                ch = getchar () ;
           }
           return f * x ;
        }
    
    class LinearBasis {
        private : int base[65] ;
        public :
            inline void clear () { MEM ( base , 0 ) ; return ; }
        public :
            inline bool insert (int x) {
                for (int i = 62 ; ~ i ; -- i)
                    if ( x & ( 1ll << i ) ) {
                        if ( base[i] ) x ^= base[i] ;
                        else { base[i] = x ; return true ; }
                    }
                return false ;
            }
    } B ;
    
    const int N = 1e3 + 100 ;
    
    int n , ans ; pii v[N] ;
    
    inline bool cmp (pii a , pii b) { return a.two > b.two ; }
    
    signed main (int argc , char * argv[]) {
        n = rint () ;
        rep ( i , 1 , n ) v[i].one = rint () , v[i].two = rint () ;
        sort ( v + 1 , v + n + 1 , cmp ) ;
        rep ( i , 1 , n ) if ( B.insert ( v[i].one ) ) ans += v[i].two ;
        printf ("%lld
    " , ans ) ;
        system ("pause") ; return 0 ;
    }
    
  • 相关阅读:
    Beta 冲刺(4/7)
    Beta 冲刺(3/7)
    Beta 冲刺(2/7)
    福大软工 · 最终作业
    福大软工 · 第十二次作业
    Beta 冲刺(7/7)
    Beta 冲刺(6/7)
    Beta 冲刺(5/7)
    Beta 冲刺(4/7)
    Beta 冲刺(3/7)
  • 原文地址:https://www.cnblogs.com/Equinox-Flower/p/11756690.html
Copyright © 2011-2022 走看看