zoukankan      html  css  js  c++  java
  • E. Maximum Subsequence Value 思维

    E. Maximum Subsequence Value

    题目大意:

    给你一个序列,让你选k个数求出这个序列的最大价值。

    序列价值的定义:假设选定了k个数,那么将这个序列的所有的数转化成二进制,那么如果二进制位置 (i) 上的1的数量 (>=max(k-2,1)) 那么序列的价值就加上 (2^i)

    题解:

    这个题目我有点看错了。。是最后求贡献的时候算这个1的数量,而不是必须满足这个1的数量大于等于 这个值才可以算贡献。。。

    • 首先可以知道如果 (k<=3) 那么就不需要判断1的数量,只要是有1 的位置肯定满足
    • 第二点,如果 (k>=4) 那么从 (k=3)(k=4) 这价值肯定不会增加,因为加一个数进来首先必须满足之前有1的位置现在这个加进来的数 (x) 也必须有,除此之外就算 (x) 有其他位置的数也没用,因为之前位置没有,所以无法构成两个。
    • 知道前面两点了就可以求解了,最后就是一个小小的贪心,从高位往低位考虑。
    #include <cstdio>
    #include <queue>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <bits/stdc++.h>
    #define id first
    #define val second
    #define inf 0x3f3f3f3f
    #define inf64 0x3f3f3f3f3f3f3f3f
    #define debug(x) printf("debug:%s=%d
    ",#x,x);
    //#define debug(x) cout << #x << ": " << x << endl
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn=550;
    const int M = 70;
    bitset<M>bit[maxn];
    ull ans[maxn];
    
    int main(){
        int n;
        scanf("%d",&n);
        ull cur = 0;
        for(int i=1;i<=n;i++){
            ll x;
            scanf("%lld",&x);
            cur |=x;
            for(ll j=0;j<=62;j++){
                if(x&(1ll<<j)) bit[i].set(j);
            }
        }
        ull Ans = 0;
        if(n<=3) Ans = cur;
        for(int i=1;i<=n;i++){
            ans[i]=bit[i].to_ullong();
            for(int j=i+1;j<=n;j++){
                for(int k=j+1;k<=n;k++){
                    bitset<M>now=bit[i]|bit[j]|bit[k];
                    ans[i]=max(ans[i],now.to_ullong());
                }
            }
            Ans=max(Ans,ans[i]);
        }
        printf("%llu
    ",Ans);
        return 0;
    }
    
    
  • 相关阅读:
    谈谈程序员、技术主管和架构师
    PyPI教程
    python以下划线开头的变量和函数的作用
    csv和xlsx区别
    编码问题2 utf-8和Unicode的区别
    编码问题1
    洛谷 1404 平均数
    洛谷 1441 砝码秤重
    【模板】高斯消元法
    线段树练习题
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/13272199.html
Copyright © 2011-2022 走看看