[Luo Gu P2114] [NOI2014] get up distress syndrome

Original title Portal

Thought this question of arithmetic or bit like pressure DP &

Features bit carry operation is not binary, so that the bit operation between participating you are independent

X0 k-th position should be filled only when there is a 1 in the following two situations:

  1. Have values ​​not more than m +1 << k
  2. Involved in computing a k-th bit of each parameter, if the initial value is 1, then n times after the operation result is 0 Similarly

In this way, we can do something like greedy thoughts, the greatest possible results

Code:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 100010;
int n, m, x;
pair<string, int> a[maxn];
inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
        f = (ch == '-') ? -1 : 1, ch = getchar();
    while (isdigit(ch))
        x = x * 10 + (ch - '0'), ch = getchar();
    return x * f;
}
inline int cal(int bit, int now)
{
    for (int i = 1; i <= n; i++)
    {
        int x = a[i].second >> bit & 1;
        if (a[i].first == "AND")
            now &= x;
        else if (a[i].first == "OR")
            now |= x;
        else
            now ^= x;
    }
    return now;
}
signed main()
{
    n = read(), m = read();
    for (int i = 1; i <= n; i++)
    {
        char str[5];
        int x;
        scanf("%s%d", str, &x);
        a[i] = make_pair(str, x);
    }
    int val = 0, ans = 0;
    for (int bit = 29; bit >= 0; bit--)
    {
        int res0 = cal(bit, 0);
        int res1 = cal(bit, 1);
        if (val + (1 << bit) <= m && res0 < res1)
            val += 1 << bit, ans += res1 << bit;
        else
            ans += res0 << bit;
    }
    cout << ans << '\n';
}

Guess you like

Origin www.cnblogs.com/wyctstf/p/11519394.html