Codeforces E. Maximum Subsequence Value (二進制 / 思維) (Round #648 Div.2)

傳送門

題意: 在給定的序列中選擇一個子序列,若子序列裏有k個數,當至少有max(1,k-2)個數在二進制第i位上爲1的時候,對答案就有2 ^ i貢獻,試問最大的答案是多少。
在這裏插入圖片描述
思路:

  • 若選定了k個數,那麼假設有 >= k - 2個數包含二進制的第i位。
  • 那現在只選擇3個數,若原來有 >= k - 2個數包含二進制的第i位,那麼這三個數種也至少有一個數包含這一位二進制,因此(枚舉找到特定的)三個數就能找到最優解。

代碼實現:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2e5 + 5;

int n, ans;
int a[505];

signed main()
{
    IOS;

    cin >> n;
    for(int i = 0; i < n; i ++)
        cin >> a[i];

    for(int i = 0; i < n; i ++)
        for(int j = 0; j < n; j ++)
            for(int k = 0; k < n; k ++)
                ans = max(ans, a[i] | a[j] | a[k]);

    cout << ans << endl;

    return 0;
}

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