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

B. And

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard 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 andoperation.

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

input

Copy

4 3
1 2 3 7

output

Copy

1

input

Copy

2 228
1 1

output

Copy

0

input

Copy

3 7
1 2 3

output

Copy

-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.

思路:此题可以分为四种情况进行分析,

        (1)0:如果 输入的数中包含两个以上的数相等

        (2)1:二进制计算之后的与 原来的数相等时

        (3)2:以上两种情况均不存在,两个二进制计算之后的数相等

        (4)-1:以上三种情况均不存在时(也就是输入的数均不相等切小于等于x)

代码:参考大佬的思路,自己的代码思路超时 唉

       f[a][0] 代表原来的数,f[a][1]代表二进制后的

#include<bits/stdc++.h>
using namespace std;
bool f[100008][2];
int main()
{
    int n,x;
    scanf("%d%d",&n,&x);
    int ans=100000;
    for(int i=0;i<n;i++)
    {
        int a;
        scanf("%d",&n);
        int b=a&x;
        if(f[a][0])///如果存在两个原来相同的输出0
        {
            ans=0;
        }
        else if (f[a][1]||f[b][0])///没有二进制之前的或者二进制之后的和之前的相比
        {
            ans=min(ans,1);///通过min函数可以保证最小的的操作
        }
        else if(f[b][1])///改变后的数存在两个相同的
        {
            ans=min(ans,2);
        }
        f[a][0]=true;
        f[b][1]=true;
    }
    if(ans==100000){
        ans=-1;
    }
    rintf("%d\n",ans);
    return 0;
}

      自己的不舍得扔的代码超时,此代码测试不过,思路可以借鉴:

 

#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
int a[100008],g[100008];
int main()
{
    int n,x,temp=-1,ans=0,t=0;
    scanf("%d%d",&n,&x);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]<=x)
        {
            ans++;
            t++;
        }
        for(int j=1;j<i;j++)///判断已有两个相等的
        {
            if(a[i]==a[j])
            {
                printf("0\n");
                return 0;
            }
        }
    }
    sort(a+1,a+n+1);
    for(int i=t+1;i<=n;i++)///当大于x的值经过二进制计算是否有与前面相等的
    {
       int w=a[i]&x;
       for(int j=1;j<i;j++)
       {
           if(a[j]==w)
           {
               printf("1\n");
              return 0;
           }
       }
    }
    for(int i=t+1;i<=n;i++)///寻找大于x的经过二进制计算存在两个相等的
    {
        g[i]=a[i]&x;
    }
    for(int i=t+1;i<=n;i++)
    {
        int w=a[i]&x;
        for(int j=t+1;j<=n;j++)
        {
            if(w==g[j]&&i!=j)
            {
                printf("2\n");
                return 0;
            }
        }
    }
    printf("-1\n");
    return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会敲代码的小帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值