zoukankan      html  css  js  c++  java
  • (补题 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;
    }
    
  • 相关阅读:
    windows 10中出现“某个应用导致****文件的默认应用设置出现问题”
    全站仪数据修正为南方cass可识别数据
    用顺序表建立一个简单的可以插入删除的学生成绩管理表
    头插法与尾插法建立单链表
    修改windows下的服务名称
    批处理同时执行多个Ant文件
    Informix数据库中的TO_DATE函数
    Ant发送邮件email
    数据库视图
    Hibernate的三种状态的解释
  • 原文地址:https://www.cnblogs.com/cafu-chino/p/11759479.html
Copyright © 2011-2022 走看看