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;
}

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