Baidu 2017 questions: Look for n employees who are not clocked in the one

Source: Internet
Author: User
Tags acos gcd

Disclaimer: Pictures from the network, the author just try to do a bit, and then make a record.

When I get this topic, I think of the first two points. Two arrays, one is a collection of all employees A; one is a collection of missing one B. To a A, B, and then two points, B to get the middle employee's work number mid, if a[mid] = = B[mid], then the absence of the employee's work number in mid, continue two points; if A[mid] < B[mid], then the absence of the employee's work number before mid, continue two points. It is worth noting that here A[mid] is not greater than b[mid]. In addition, the two points here are only for the absence of work number in the middle of the case. If the absence of the work number in the array (of course, after sorting) the end, alone can be processed.

1#include <bits/stdc++.h>2 using namespacestd;3 4typedefLong LongLL;5 #defineMOD 1000000007LL6 #definePI ACOs (-1.0)7 Const DoubleEPS = 1e-6;8 //Const Double PI = ACOs ( -1.0);9 Const intINF =0x3f3f3f3f;Ten //const LL MOD = 1e9+7; One  ATemplate <classT>inline T Bigmod (t P, t e, T M) { -     Long LongRET =1; -      for(; e >0; E >>=1){ the         if(E &1) ret = (ret * p)%M; -p = (p * p)%M; -}return(T) ret% M;//attention:bigmod (p, 0, 1), so ret have to module M. - } +Template <classT> inline T Modinverse (t A, t M) {returnBigmod (A, M2, M);} -Template <classT> inline T gcd (t A, T b) {returnB? GCD (b, a%b): A;} + intMain () { A     intT; atscanf"%d", &T); -      for(inti =1; I <= T; ++i) { -vector<int>Total ; -vector<int>arrive; -         intNum CIN >>num; -          for(intj =0; J < num; ++j) { in             intEmployee CIN >>employee; - Total.push_back (employee); to         } +          for(intj =0; J < Num-1; ++j) { -             intarriver; CIN >>arriver; the Arrive.push_back (arriver); *         } $         /** * Binary searchPanax Notoginseng sort (Total.begin (), Total.end ()); - sort (Arrive.begin (), Arrive.end ()); the if (total[0]! = arrive[0]) cout << total[0] << Endl; + else if (total[num-1]! = arrive[num-2]) cout << total[num-1] << Endl; A else { the int left = 0, right = num-2; + While (left <= right) { - int mid = left + (right-left)/2; $ if (total[mid] = = Arrive[mid]) { $ Left = mid + 1; - }else if (Total[mid] < Arrive[mid]) { - Right = mid-1; the                 } -             }Wuyi cout << Total[left] << Endl; the         }*/ -         intNUM1 =0; Wu          for(Auto iter = Total.begin (); ITER! = Total.end (); + +ITER) { -NUM1 = num1 ^ *iter; About         } $         intnum2 =0; -          for(Auto iter = Arrive.begin (); ITER! = Arrive.end (); + +ITER) { -num2 = num2 ^ *iter; -         } Acout << (num1 ^ num2) <<Endl; +     } the     return 0; -}

Then a friend came up with a better way, and it was pretty. Use the idea of XOR.

Let's look at an expression like this: p ^ q = m, M equals P,q. So there is, p = q ^ m, q = P ^ M.

Now, look back at the subject. We make the elements in a different or get m, and the elements in B differ from each other or get p. So, which employee number is q=m^p.

The implementation of the code is also integrated in the appeal code.

Baidu 2017 questions: Look for n employees who are not clocked in the one

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.