LuoGu: CF1208F Bits And Pieces
CF: F. Bits And Pieces
从后向前枚举 $i$ ,贪心从高位取,如果后面有两个及以上的数含有子集当前这个数,那么可以取。
每次 check 答案后,将所有当前数的子集打上标记,直接枚举所有的子集会 T 飞,如果已经有两个标记,不必在继续,这样均摊 $O(n)$ 。
总复杂度 $O(n \log n)$ 。
另外这里有一个枚举子集的 trick :
1 2
   | for (int i = x; i; i = i - 1 & x)     s[i]++;
   | 
 
 查看代码 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
   | #include <cstdio> #include <algorithm> using namespace std; template <class Type> void read(Type &x) {     char c;     bool flag = false;     while ((c = getchar()) < '0' || c > '9')         c == '-' && (flag = true);     x = c - '0';     while ((c = getchar()) >= '0' && c <= '9')         x = (x << 3) + (x << 1) + c - '0';     flag && (x = ~x + 1); } template <class Type> void write(Type x) {     x < 0 && (putchar('-'), x = ~x + 1);     x > 9 && (write(x / 10), 0);     putchar(x % 10 + '0'); } const int N = 5e6 + 10; int n, w[N], s[N], vis[N]; void add(int x, int t) {     if (s[x] > 1 || vis[x] == t)         return;     s[x]++, vis[x] = t;     for (int i = 0; (1 << i) <= x; i++)         x >> i & 1 && (add(x ^ 1 << i, t), 0); } int main() {     read(n);     for (int i = 1; i <= n; i++)         read(w[i]);     add(w[n], n), add(w[n - 1], n - 1);     int res = 0;     for (int i = n - 2; i; i--)     {         int k = 0;         for (int j = 20; ~j; j--)             if (!(w[i] >> j & 1) && s[k ^ 1 << j] > 1)                 k ^= 1 << j;         res = max(res, w[i] | k);         add(w[i], i);     }     write(res);     return 0; }
   |