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;
    }
    
  • 相关阅读:
    WINCE/WM5.0如何让安装完后自动运行程序
    Android 图片透明度处理代码
    windows mobile UI 自定义开始菜单图标
    Windows mobile 6捕获键盘操作
    HTTP的post和get总结
    提供一个Windows mobile Native UI 程序,循序渐进开发,并附有代码!
    Windows Mobile 中ComboBox【下拉列表】的使用
    注册表修改今日桌面左右软键的功能
    .net compact framework 注册表操作
    两种实现Toast 的例子(图片&文字)
  • 原文地址:https://www.cnblogs.com/cafu-chino/p/11759479.html
Copyright © 2011-2022 走看看