zoukankan      html  css  js  c++  java
  • [CF1365E] Maximum Subsequence Value

    [CF1365E] Maximum Subsequence Value - 贪心

    Description

    给出一个长度为 (n) 的数列 (a),你需要选出一个子序列,使其价值最大,输出最大的价值。对于一个长度为 (k) 的子序列,若在这个子序列中有不少于 (max(1,k-2)) 个数的二进制位 (i) 上是 (1),则其价值增加 (2^i)

    Solution

    考虑当 (k le 3) 时,选择所有的数一定最优。

    (k > 3) 时,由于选择的数每增加一个,每一位要产生贡献要求的数的个数也会增加一个,这样无论如何都不会变得更优,因此我们只需要枚举选择三个数即可。

    换言之,简单地说,当且仅当 (k le 3) 时我们是在做或运算。

    #include <bits/stdc++.h>
    using namespace std;
    
    #define int long long
    const int N = 1e+6 + 5;
    const int M = 1e+3 + 5;
    const int mod1 = 1e+9 + 7;
    const int mod2 = 998244353;
    
    #define dbg(x) cerr << #x << ":" << x << endl
    
    void solve()
    {
    	int n;
    	cin >> n;
    	vector<int> a(n + 2);
    	for (int i = 1; i <= n; i++)
    		cin >> a[i];
    
    	if (n <= 3)
    	{
    		int ans = 0;
    		for (auto i : a)
    			ans |= i;
    		cout << ans << endl;
    	}
    	else
    	{
    		int ans = 0;
    		for (int i = 1; i <= n; i++)
    		{
    			for (int j = 1; j <= n; j++)
    			{
    				for (int k = 1; k <= n; k++)
    				{
    					int tmp = a[i] | a[j] | a[k];
    					ans = max(ans, tmp);
    				}
    			}
    		}
    		cout << ans << endl;
    	}
    }
    
    signed main()
    {
    	ios::sync_with_stdio(false);
    
    	int t;
    	t = 1;
    	while (t--)
    	{
    		solve();
    	}
    
    	return 0;
    }
    
  • 相关阅读:
    textarea输入字符有限制
    linux 简单命令
    jQuery animate()
    两张图切换
    表单验证 靠name获取
    jquery验证手机号码
    倒计时
    锚点链接 阻止a标签跳转
    滚动监听: bootstrap 的scrollspy
    MySQL 02
  • 原文地址:https://www.cnblogs.com/mollnn/p/14170459.html
Copyright © 2011-2022 走看看