Binary template // The Frog's Games + Aggressive cows

  Templates for different output binary case where two different topics listed below as

 

                                                                                             The Frog's Games  

code show as below

               

                                                               

                                                    // binary special training
                                                #include <stdio.h> // lower_bound it is greater than or equal to the first number
                                                #include <iostream> // upper_bound first number is greater than its
                                                #include <algorithm>
                                                #include <string.h>
                                                STD namespace the using;
                                                int A [500 005];
                                                int n-, m; r // L long, n-stone seat, may hop m times
                                                BOOL LPT (int WF)
                                                {
                                                      int pos=0;
                                                for(int x=1;x<=m;x++)
                                                {
                                                      pos=upper_bound(a,a+n,a[pos]+wf)-a-1;
                                                     if(pos>=n-1)
                                                     return true;
                                                }
                                                      return false;
                                               }
                                               int main()
                                               {
                                                     int l,i,j,left,right,mid;
                                                  while(scanf("%d%d%d",&l,&n,&m)!=EOF)
                                               {
                                                     int minn=1e9;
                                                     for(i=1;i<=n;i++)
                                                     scanf("%d",&a[i]);
                                                     sort(a,a+n+1);
                                                     a[0]=0;a[n+1]=l;
                                                     n=n+2;
                                                     right=l;
                                                     left=l/m;
                                                   while(left<=right)
                                               {
                                                      mid=(left+right)/2;
                                                       if(lpt(mid))
                                                  {
                                                           right=mid-1;
                                                           minn=min(minn,mid);
                                                  }
                                                       else
                                                     left=mid+1;
                                              }
                                                     printf("%d\n",minn);
                                              }
                                                     return 0;

                                            }
                                                                                                             Aggressive cows   

                                   code show as below

                                     

                                       // binary special training
                                       #include <stdio.h> // lower_bound it is greater than or equal to the first number
                                       #include <iostream> // upper_bound first number is greater than its
                                       #include <algorithm>
                                       #include <string.h>
                                       the using namespace STD;
                                       int A [100005];
                                       int n-, C;
                                       BOOL LPT (int X)
                                      {
                                         int D, J, ANS = 0; D = A [0];
                                          for (J =. 1; J <= n--. 1 ;j++)
                                        {
                                           if(a[j]-d>=x)
                                          {
                                             ans++;
                                             d=a[j];
                                          }
                                        }
                                          if(ans<c-1) return true;
                                                    else return false;
                                     }
                                     int main()
                                    {
                                        int maxm=0,i,m,mid,left,right;
                                        scanf("%d%d",&n,&c);
                                        for(i=0;i<n;i++)
                                        scanf("%d",&a[i]);
                                        sort(a,a+n);//1 2 4 8 9
                                        left=0;
                                        right=(a[n-1]-a[0])/(c-1);//4
                                       while(left<=right)
                                      {
                                          mid=(left+right)/2;
                                         if(lpt(mid))
                                        {
                                          right=mid-1;
                                        }
                                          else
                                       {
                                           left=mid+1;
                                           maxm=max(maxm,mid);
                                        }
                                      }
                                       printf("%d\n",maxm);
                                      return 0;
                                   }

                               c language code - beginners learn together ,,

Guess you like

Origin www.cnblogs.com/lptlptlpt/p/11830128.html