Half a small note

The basic use of binary sequences is monotone monotonic function or a lookup operation to do.

Two points, as the name suggests, is to continue to be solved were divided into two sections, to determine the target range according to the situation where the element solving interval midpoint, so put Solutions range reduced by half.

Binary complexity of the algorithm: O (binary number * Complexity single determination)

Half wording:

Binary integer fields:
 int erfen (here, may be added as required parameter) {
     int L = half the lower bound, r = bound on two points, ANS;
     the while (L <= R & lt) {
         int MID = (L + R & lt) >> . 1 ;
         IF (Check (MID)) ANS = MID, L = MID + . 1 ; // where seeking is the minimum of the biggest problems
           the else R & lt = mid- . 1 ; 
    } 
    return ANS; 
} 

bipartite real domain: 
Double erfen (here as needed add parameters) {
     Double L = half the lower bound, r = the half bound, ANS;
     the while (FABS (LR)> DLT) {
         Double MID = (L + R & lt) >> . 1 ;
         IF(check(mid)) l=mid+1;
          else r=mid-1;
    } 
    return l;
}

Half common questions:

1. binary answer: the smallest maximum, minimum, the biggest problem:

I give chestnuts:

Sequence segments Ⅱ

Typical binary problem, find the smallest maximum problem;

First-half to determine upper and lower bounds: the upper bound obviously does not exceed the number of columns and all the numbers, the lower bound is the largest of these numbers;

Binary, sets of templates, because the problem is the smallest maximum value can not be set above half template, requires a small change that, if the answer is determined bipartite feasible, then the two points from l to mid-1;

In fact, feeling half hard on the check function (at least I think so qwq), because the topics required to be contiguous can add and so we from a [1] began to increase, when one of a [k] after being together more than two scores, and we another counter ++, expressed the need for a set of subdivision (Tip: counter 1 to start recording from instead of 0), the number of segments to be divided if counter = <, refers to sub feasible, otherwise not feasible ;

#include<bits/stdc++.h>

using namespace std;

int n,m,sum,mm;
int a[100010];

bool check(int z){
    int s=0,k=1;
    for(int i=1;i<=n;i++){
        if(s+a[i]>z){
            s=a[i];
            k++;
        }
        else s+=a[i];
    }
    if(k<=m) return 1;
    else return 0;
}

int erfen(){
    int l=mm,r=sum,ans=0;
    while(l<=r){
        int mid=(l+r)>>1;
        if(check(mid)) ans=mid,r=mid-1;
          else l=mid+1;
    }
    return ans;
}

int main(){
    scanf("%d%d",&n,&m);
    
    for(int i=1;i<=n;i++)
      scanf("%d",&a[i]),sum+=a[i],mm=max(mm,a[i]);
    
    cout<<erfen()<<endl;
}

2. binary search;

General application binary search in an ordered sequence.

Do not make too much explanation qwq

 

Guess you like

Origin www.cnblogs.com/zhuier-xquan/p/10987682.html