(Coderforces 1013B)B.And 贪心 + 思维

11 篇文章 0 订阅

B. And
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

There is an array with n elements a1, a2, …, an and the number x.

In one operation you can select some i (1 ≤ i ≤ n) and replace element ai with ai & x, where & denotes the bitwise and operation.

You want the array to have at least two equal elements after applying some operations (possibly, none). In other words, there should be at least two distinct indices i ≠ j such that ai = aj. Determine whether it is possible to achieve and, if possible, the minimal number of operations to apply.

Input
The first line contains integers n and x (2 ≤ n ≤ 100 000, 1 ≤ x ≤ 100 000), number of elements in the array and the number to and with.

The second line contains n integers ai (1 ≤ ai ≤ 100 000), the elements of the array.

Output
Print a single integer denoting the minimal number of operations to do, or -1, if it is impossible.

Examples
inputCopy
4 3
1 2 3 7
outputCopy
1
inputCopy
2 228
1 1
outputCopy
0
inputCopy
3 7
1 2 3
outputCopy
-1
Note
In the first example one can apply the operation to the last element of the array. That replaces 7 with 3, so we achieve the goal in one move.

In the second example the array already has two equal elements.

In the third example applying the operation won’t change the array at all, so it is impossible to make some pair of elements equal.

题意:
有一个大小为n的数组,它的元素为a1, a2, …, an ,还有一个数x
现在你可以进行多次操作,每一次你可以将其中一个数ai变成( ai & x),问你最少进行多少次操作可以使得这个数组中至少有两个数相同,问你最少进行的操作数是多少,若不可能则输出-1.

分析:
本题最大的坑点就是读题了。。。。看你如何理解下面这句话的some的意思了。
In one operation you can select some i (1 ≤ i ≤ n) and replace element ai with ai & x…
最初我一直将其理解为一些,所以认为答案只有0,1,-1这三种,一直W….

在正确理解题意之后,我们知道,ai和x进行一次或多次 & 操作的结果是一样的,所以每个数至多改变一次。并且题目只需要两个数相同即可,所以若有可能使得某两个数相同,那么我们只需要对这两个数进行操作即可,所以至多改变两个数,因此答案只可能是0,1,2,-1,它们分别表示:

0 : And 之前的a[i] 和 And 之前的a[j] 相同,不需要进行操作

1 : And 之后的a[i] 和 And 之前的a[j] 相同,只需要改变a[i]即可,或者And 之前的a[i] 和 And 之后的a[j] 相同 ,只需要改变a[j]即可,进行一次操作

2 : And 之后的a[i] 和 And 之后的a[j] 相同,同时改变a[i]和a[j]即可,进行两次操作

-1 : 不可能使得两个数相同

每个数只有两种状态,所以我们用f[a][0]表示数a没有改变之前的状态,用f[a][1]表示数a进行&x操作之后的状态。所以上述前三种情况分别对应以下三种情况(b为a进行&x操作后的结果值):
f[a][0] = true : a改变之前和某个数改变之前相同
f[a][1] || f[b][0] : a改变之前和某个数改变之后相同,或者a改变之后和某个数改变之前相同
f[b][1]: a改变之后和某个数改变之后相同

AC代码:

#include<bits/stdc++.h>
using namespace std;
bool f[100010][2];
int main(){
    int n,x;
    cin>>n>>x;
    int ans=100000;
    for(int i=0;i<n;i++){
        int a;
        cin>>a;
        int b=a&x;
        if(f[a][0]){
            ans=0;
        }else if(f[a][1]||f[b][0]){
            ans=min(ans,1);
        }else if(f[b][1]){
            ans=min(ans,2);
        }
        f[a][0]=true;
        f[b][1]=true;
    }
    if(ans==100000){
        ans=-1;
    }
    cout<<ans;
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值