Enumerations and two-point

Source: Internet
Author: User

Everything starts from the beginning, and today we'll review the enumeration and the dichotomy. Enumeration is not much to say, say two points, two points is actually a kind of division, is when N=2 is a situation, also called binary search (binary find), two points to find faster, faster than you can check the first one, binary search is to split the set in two, see what you are looking for in the front half or the latter half, If you divide the first half in half and so on before you know what you want, in the latter half the same. One of the conditions for binary lookup is that the element table must be an ordered table. The following two templates

1 intL,r,res;//L is the left boundary, R is the right boundary, the specific value to see, res for recording mid value2  while(L<=R) {//The loop condition is important when the L>r loop ends and res records the value of L=r3     intMid= (L+R)/2;4     if(OK (mid)) {//OK function to see the specific design of the topic5L=mid+1;//choose which one to read6         //r=mid-1;7res=mid;8     }9     Else Tenr=mid-1; One         //l=mid+1; A}
1 //The first one is the two-part, the following is the floating-point binary2 #defineEPS 1e-8//defining macro Eps,eps is precision3 DoubleL,r;4  while(l+eps<R) {5     DoubleMid= (L+R)/2.0;//Note that except for the 2.06     if(OK (mid)) {7L=mid;//Note that there is no minus one or one plus8         //R=mid;9     } Ten     Else OneR=mid; A         //L=mid; -}
A question I just made two days ago. Test instructions is to give you a shape variable N to find the number of special numbers (0<n<=10000000) from within the 1~n, special number is not a duplicate number, but there are leading 0, such as 023,1124, 1234 is. This problem is not difficult, but there is time limit, so the key is to look at the decision is special number optimization.
1#include <cstdio>2#include <cstring>3 #defineMAXN 100000004 using namespacestd;5 inta[5000000];///special number is saved.6 ///Judging whether it is a special number7 BOOLOkintx) {8     BOOLvis[Ten],flag=true;///Assuming that this number has a maximum of 10 digits, the effect of vis is to record whether or not his number has been9memset (Vis,false,sizeof(VIS));//first initialized to never appear.Ten      while(x) { One         inttmp=x%Ten;///take the number of bits per digit Ax=x/Ten; -         if(Vis[tmp]) { -flag=false;///If this number ever appears, then this number is not a special number. the              Break; -         } -vis[tmp]=true;///Otherwise, the record appears. -     } +     returnFlag; - } + intMain () { AMemset (A,0,sizeof(a)); at     intCnt=0; -      for(intI=1; i<=maxn; i++) {///first, all the special numbers are stored sequentially in the array, which is the enumeration -             if(ok (i)) -A[cnt++]=i;///Cnt+1 is the number of all special numbers -     } -     intN; in      while(~SCANF ("%d",&N)) { -         intL=0, r=cnt-1, Res; to         if(n<=1){ +printf"%d\n",0); -             Continue; the         } *          while(L&LT;=R) {///is subscript, because the number of special numbers less than n is the subscript +1 of the largest special number in a tree less than N. (Think about it, because the special number is stored sequentially) $             intMid= (L+R)/2;Panax Notoginseng             if(a[mid]<N) { -L=mid+1; theres=mid; +             } A             Else ther=mid-1; +         } -printf"%d\n", res+1); $     } $}







Enumerations and two-point

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.