codeforce 165E Compatible Numbers

  题意 有集合 {ai} i = 1,2,3,...n 找出集合里面是否有与之相容的,x&y == 0  也就是,已知存在一个二进制数字 x (相当于有n个1组成),寻找一个 y (有 1 的地方跟 x 不一样)。

  当然,在R里有很多满足题意的 y (1放的位置不同,以及1的数量不同),所以从inf开始往前将 x 提供给所有可以扩展给 y (添加几个1的数)

#include <stdio.h>
#include <string.h>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;
const int inf = (1<<22);
const int maxm = 4e6+10;
const int maxn = 1e7+10;
int a[maxn];
int b[inf];
int n, m;
int main () {
        scanf("%d",&m);
        for (int i = 1; i <= m; i++) {
            scanf("%d", &a[i]);
            b[inf-a[i]-1] = a[i];
        }
        for (int i = inf-1; i >= 0; i--) {
            if (!b[i]) {
                for (int j = 0; j < 22; j++) {
                    if (b[i|(1<<j)]) {
                        b[i] = b[i|(1<<j)];
                    }
                }
            }
        }
          for (int i = 1; i <= m; i++) {
            if (i-1) {
                printf(" ");
            }
            if (b[a[i]]) {
                printf("%d", b[a[i]]);
            } else {
                printf("-1");
            }
        }puts("");
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/Urchin-C/p/11233711.html
今日推荐