【题目链接】
【思路要点】
- 从高位向低位贪心即可,遵从“使答案最大的情况下花费最小”的原则。
- 时间复杂度\(O(NLogM)\)。
【代码】
#include<bits/stdc++.h> using namespace std; const int MAXN = 100005; const int MAXLOG = 30; template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } int opt[MAXN], num[MAXN][MAXLOG]; int func(int n, int t, int x) { for (int i = 1; i <= n; i++) { if (opt[i] == 0) x &= num[i][t]; if (opt[i] == 1) x ^= num[i][t]; if (opt[i] == 2) x |= num[i][t]; } return x; } int main() { int n, m; read(n), read(m); char s[5]; for (int i = 1; i <= n; i++) { scanf("\n%s", s); int x; read(x); if (s[0] == 'A') opt[i] = 0; else if (s[0] == 'X') opt[i] = 1; else opt[i] = 2; for (int j = 0; j < MAXLOG; j++) { num[i][j] = x & 1; x >>= 1; } } int used = 0, ans = 0; for (int i = MAXLOG - 1; i >= 0; i--) { int tmp = 1 << i; if (used + tmp > m) { ans += tmp * func(n, i, 0); continue; } int one = func(n, i, 1), zero = func(n, i, 0); if (one == zero) ans += one * tmp; else if (one == 1 && zero == 0) { ans += tmp; used += tmp; } else ans += tmp; } writeln(ans); return 0; }


1万+

被折叠的 条评论
为什么被折叠?



