CSU 1323 asks a heap of numbers to take out 2 xor so that the XOR value is greater than a certain number

Source: Internet
Author: User
Tags time limit
1323:zzy and his little friendsTime Limit:5 Sec Memory limit:256 MB
submit:239 solved:103
[Submit] [Status] [Web Board] Description

Zzy raised a little monster and N Bump man, singled out every bump is not the opponent of the small monster, so must be two bump-man cooperation to fight with the small monsters. The combat effectiveness of bump man A and bump man B is the difference or value of their combat effectiveness. Now it is up to the zzy to fight the little monsters by electing two from the N-Bump Mann. May I ask whether Zzy can elect two of them so that they can defeat the little monsters (they are bigger than the small monsters). Input

Input has multiple examples until the end of the file. The first line of each example contains two numbers n and M, which indicates that there are N (2<=n<=100000) only bump Mann, the small Monster's combat capacity is M (0<m<=1000000000). Then there is a row n number, each number Ai (0<AI<M) represents the combat effectiveness of each bump. Output

For each example output line, if you can pick two bumps to make them beat the little monster output "YES", otherwise output "no" (without quotation marks) Sample Input

2 5
1 1
2 6
5 2
Sample Output
NO
YES
HINT

Csu_cx

Source

CSU Monthly Oct.

The idea of a dictionary tree can not enumerate all the points for 22 to see the XOR value, we know that when the two digits are expressed as binary when the difference is its XOR value is 1, thus can get O (n) algorithm, the first to save all the number into the dictionary tree (for quick query), when using a number to go with the other number XOR, The maximum difference or value can be obtained directly according to the greedy strategy (choose different digits as far as possible).

#include <stdio.h> int n,m;
      struct haha {int next[3];
int nm;
}NODE[100005*4];
int POS;
    int makend () {int i;
    pos++;
    for (i=0;i<2;i++) node[pos].next[i]=-1;

    node[pos].nm=0;

return POS;
    } void _insert (int x) {int cur=0,i;
        for (i=0;i<30;i++) {int flag= (x& (1<<i))? 1:0;
        if (node[cur].next[flag]==-1) node[cur].next[flag]=makend ();
    Cur=node[cur].next[flag];
} node[cur].nm=x;
    } int findd (int x) {int cur=0,i;
        for (i=0;i<30;i++) {int flag= (x& (1<<i))? 1:0;
             if (flag==1) {if (node[cur].next[0]!=-1) cur=node[cur].next[0];
        else cur=node[cur].next[1];
               } else if (flag==0) {if (node[cur].next[1]!=-1) cur=node[cur].next[1];
        else cur=node[cur].next[0];
}} return x^node[cur].nm; } int Mmax (int a,int b) {REturn a>b?a:b;
     } int main () {int i,x;
         while (scanf ("%d%d", &n,&m)!=eof) {int mx=0;
         pos=0;
            for (i=0;i<2;i++) node[pos].next[i]=-1;
         node[pos].nm=0;
             for (i=0;i<n;i++) {scanf ("%d", &x);
             if (i!=0) Mx=mmax (MX,FINDD (x));
         _insert (x);
           } if (mx>m) printf ("yes\n");
     else printf ("no\n");
} return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.