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