NOJ---1567 --- Binary Search

Source: Internet
Author: User

My code has not been submitted on OJ because our OJ crashes again =-=

Touch me

= He just handed in, but it should be right, because the great God helped me solve the endless loop problem.

L = Mid + 1 and L = mid

In a strict sense, it is still different. When my condition is --- mid = (L + r)/2, then it is biased towards L and should adopt the first method.

If mid = (L + R + 1)/2, R should be written as mid-1

 

The second question is not difficult.

The basic framework of binary is similar to that of binarysearch.

This still depends on the specific question =-=

 1 #include <iostream> 2 using namespace std; 3  4 int n , m; 5 const int size = 500010; 6 int arr[size]; 7  8 bool bSearch( int x ) 9 {10     int cnt;11     cnt = 0;12     for( int i = 0 ; i<n ; i++ )13     {14         cnt += ( arr[i] + (x - 1) ) / x;15     }16     return cnt<=m;17 }18 19 int main()20 {21     int l , r;22     int mmax;23     while( ~scanf("%d %d",&n,&m) )24     {25         mmax = 0;26         if( n==-1 && m==-1 )27             break;28         for( int i = 0 ; i<n ; i++ )29         {30             scanf( "%d",&arr[i] );31             if( arr[i]>mmax )32             {33                 mmax = arr[i];34             }35         }36         l = 0;37         r = mmax;38         while( l<r )39         {40             int mid = l+(r-l)/2;41             if( bSearch(mid) )42             {43                 r = mid;44             }45             else46             {47                 l = mid+1;    48             }49         }50         cout<<l<<endl;51     }52     return 0;53 } 
View code

 

Today:

I don't know if we each have a destiny, or just floating around-like on a breeze

 

 

 

 

 

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.