Sequence segments Section II

Binary answer 
question is intended: the number n is divided into m segments seeking a solution, so that the maximum and minimum of the m segments in 
    fact, that each segment and each program has a corresponding maximum value,,
    requires a scheme the maximum value of minimum

Solution: The answer mid-half of the maximum value of the share, 
then O (n) to determine the feasibility answer 
greedy to do so, it will be over if coupled with, and it opened a new paragraph

Finally, the number of stages is smaller than the judgment of the m

1. Note To determine if this value is greater than the current mid, has a value greater than mid, and then exit
, otherwise, the value will only be considered as a separate segment, in fact, he does not fit for some

2, or also this method: maximum answers a [i] begins the enumeration of

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <iomanip>
#include <iostream>
using namespace std ; 

const int maxn = 100011 ; 
int n,m,l,r,mid ; 
int a[maxn] ; 

inline bool check( int mid ) 
{
    int ans = 0,sum = 0 ; 
    for(int I = . 1 ; I <= n-; I ++ ) 
    { 
        IF (A [I]> mid) return  0 ;  
 // . 1, if the note is determined to be greater than the current value mid, has a value greater than the mid,
 // that it will exit, otherwise, the value will only be considered as a separate segment, in fact, he does not fit some 
        IF (+ a [i] SUM> MID) 
            ANS ++, SUM = 0 ;   
        SUM + = a [i] ; 
    } 
    ANS ++ ; 
     return ANS <= m;   
} 

int main () 
{ 
    Scanf ( " % D% D " , & n-, & m); 
     for(int i=1;i<=n;i++) scanf("%d",&a[ i ]) ; 
    l = 0,r = 1e9 ; 
    while( l<r ) 
    {
        mid = ( l+r )>>1 ;
        if(check(mid)) 
            r = mid ; 
        else  
            l = mid + 1 ;
    }
    printf("%d\n",r) ; 
    return 0 ; 
}

 

Guess you like

Origin www.cnblogs.com/lcezych/p/10992739.html