codeforces E. Maximum Subsequence Value

在这里插入图片描述

题目

题意:

给你一个序列aa,对于每一个aia_i都对应着一个二进制,我们可以选择kk个数字,我们需要找出这些数字中每个位子至少有max(1,k2)max(1, k-2)11的位子,然后化成2x1+2x2.....2^{x1}+2^{x2}.....,如何选择这些数字使其最大,问最大是多少。

思路:

我们可以分析一下不同的情况:

  • k=1k=1的时候,那答案肯定就是nn了。
  • k=2k=2的时候,那么每个位子上只要有一个至少一个11存在就行了,那么也可以直接a1a2a_1 | a_2
  • k=3k=3的时候,道理同上
  • k>3k>3的时候,那么现在每个位子上所需要11的就会越来越多,试想一下,虽然每次都可以多选择一个数字,但是每个位子所需要的11也要越来越多,那么如果这个数字能够增加的话,那么为什么我们在选择33个数字的不选择这个数字,因为如果要增加的,那么就是这个数能覆盖前面一个数字的基础上才可以增加,但是这样的话,可以在刚开始就选择这个数,所以kk最大的时候,选择33
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <cctype>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
int xxxxxxxxx = 1;
inline void outi(int x) {if (x > 9) outi(x / 10);putchar(x % 10 + '0');}
inline void outl(ll x) {if (x > 9) outl(x / 10);putchar(x % 10 + '0');}
inline void debug(ll x) {cout << xxxxxxxxx++ << " " << x << endl;}
inline void debugs(string s) {cout << s << endl;}
const int maxn = 510;
ll a[maxn] = {0};
int main() {
    ll n; read(n); for (int i = 1; i <= n; i++) read(a[i]);
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            for (int k = 1; k <= n; k++) ans = max(ans, a[i] | a[j] | a[k]);
        }
    }
    outl(ans);
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章