Codeforces Round #500 (Div. 2) [based on EJOI] B - And

http://codeforces.com/contest/1013/problem/B

题意:给出一个数x和n个数,问这n个数能否通过与x进行&操作出现两个相同的数

若能输出最短的变化次数

有这几种情况:

0次:n个数中原本就有相同的数

1次:一个数通过一次变化变成了另一个数

2次:有两个原本不同的数通过各一次变换变成了同一个数

无解:不论如何变换不会出现相同的数

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
        multiset<int> s1, s2;
        set<int> s3, s4;
        int n, k;
        int flag = 0;
        while (cin >> n >> k) {
                s1.clear(); 
                s2.clear();
                s3.clear();
                s4.clear();
                flag=0;
                int num;
                for (int i = 0; i < n; i ++) {
                        scanf("%d",&num);
                        s1.insert(num);
                        s3.insert(num);
                        if ((num&k) != num) {
                                s2.insert(num&k);
                                s4.insert(num&k);
                        }
                } 
                if (s1.size() != s3.size()) {
                        cout << '0' << endl;
                        continue;
                }
                for (auto it = s1.begin(); it != s1.end(); it ++) {
                        if (s4.count(*it)) {
                                flag = 1;
                                cout << '1' << endl;
                                break;
                        }
                }
                if (flag) continue;
                if (s2.size() != s4.size()) {
                        cout << '2' << endl;
                        continue;
                }
                cout << "-1" << endl;
                
        }
        return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值