• (补题 CF 1013B 模拟)And


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

    In one operation you can select some i (1 ≤ i ≤ n) and replace element a**i with a**i & 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 a**i = a**j. 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 a**i (1 ≤ a**i ≤ 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

    4 3
    1 2 3 7
    

    Output

    1
    

    Input

    2 228
    1 1
    

    Output

    0
    

    Input

    3 7
    1 2 3
    

    Output

    -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的数组和一个数x,你可以用x按位与数组中的一个数替换掉对应位置的数(当然你也何以不替换),使操作后数组中至少有两个相同的元素。如果能则输出操作次数,否则输出-1;

    代码样例(本题实质大模拟)

    #include<iostream>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #include<stdio.h>
    using namespace std;
    
    struct node{
        int k,l;
    };
    
    bool cmp(node q,node p){
        return q.l<p.l;
    }
    
    int main(){
        int n,x;
        cin>>n>>x;
        node a[100010];
        node b[100010];
        int t=0;
        int sum=0;
        for(int i=0;i<n;i++){
            a[i].k=i;
            b[i].k=i;
            cin>>a[i].l;
            b[i].l=a[i].l&x;
        }
        sort(a,a+n,cmp);
        sort(b,b+n,cmp);
        for(int i=0;i<n-1;i++){
            if(a[i].l==a[i+1].l){
                sum=0;
                cout<<sum<<endl;
                return 0;
            }
        }
        int i=0,j=0;
        while(1)
        {
            if(a[i].l==b[j].l&&a[i].k!=b[j].k)
            {
                sum=1;
                cout<<sum<<endl;
                return 0;
            }
            if(a[i].l==b[j].l&&a[i].k==b[j].k)
            {
                j++;
                continue;
            }
            if(a[i].l>b[j].l) {
                j++;
            }
            else{
                i++;
            }
            if(i==n||j==n){
                break;
            }
        }
        for(i=0;i<n-1;i++){
            if(b[i].l==b[i+1].l){
                sum=2;
                cout<<sum<<endl;
                return 0;
            }
        }
        cout<<-1<<endl;
    }
    
  • 相关阅读:
    Java finally语句到底是在return之前还是之后执行?
    图文介绍openLDAP在windows上的安装配置
    ES之十:ElasticSearch监控工具 cerebro
    ES之八:ES数据库重建索引——Reindex(数据迁移)
    ES index的数据量大于1万的特殊处理场景
    Java中的Hashtable实现方法
    form表单回车提交问题(转)
    ES之九:ES Java客户端Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)
    NetBeans 7.0 安装Python插件的方法
    python学习笔记
  • 原文地址:https://www.cnblogs.com/cafu-chino/p/11759479.html
Copyright © 2020-2023  润新知