usaco dichotomy problem cases

【description】

Total N  (. 1 ≤  N  ≤ 100,000) days, sub- M  (. 1 ≤  M  ≤  N  ) th clearing months

May contain a liquidation of a working day or more consecutive days, every weekday only be included in a clearing among the month.

Sequence packets, to obtain a maximum monthly expenditure is minimized (i.e., resulting in a maximum of all minimum possible grouping of results).

[Sample input]

7 5
100
400
300
100
500
101
400

[Sample Output]

500

 

solution:

 

#include <bits / STDC ++ H.>
 the using  namespace STD;
 int n-, m, ANS, A [ 100005 ];
 int Judge ( int MID) 
{ 
    int SUM = 0 , CNT = . 1 ;
     for ( int I = . 1 ; I < = n-; I ++ ) 
    { 
        IF (+ a [I] <= MID) sUM + = sUM a [I];
     /// connected added into one group until the sum of the group is greater than the MID 
        the else 
        { 
            sUM = a [I]; CNT ++ ;   /// CNT note of the number of groups from the sum a [i] plus restarted even 
            IF (CNT> m || sum> MID) return  0 ;
    /// If m exceeds the number of sets or returns 0 at a higher cost than the mid 
        } 
    } 
    return  . 1 ; 
} 
int main () 
{ 
    Scanf ( " % D% D " , & n-, & m);
     int Le = 0 , RIG = 0 ;
     for ( int I = . 1 ; I <= n-; I ++ ) { 
        Scanf ( " % D " , & A [I]); 
        Le = min (Le, A [I]); 
        RIG + = A [I] ; 
    } 
    the while (Le <= RIG)    /// shrinking range up to find an answer
    {
         Int mid = (Le + RIG) >> . 1 ;
     /// taking a median of a small mid le = mid + 1 to find to find the right or to the left rig = mid-1 
        IF (Judge (mid))   // / if returned to 1 mid> = answer 
        { 
            ANS = MID;   /// first save MID 
            RIG = mid- . 1 ;
     /// only the case if mid = answers rig = mid-1 continues to circulate in the Judge () entering else until the part out of the loop. Otherwise, the big mid continue to shrink until mid = answers. 
        }
         The else Le = mid + . 1 ; /// If the return takes 0 in a small mid larger than mid 
    } 
    the printf ( " % D \ n- " , ANS); 

    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/lightworkshopnoi/p/11416263.html