Half a brief description examples

The first question angry cow

This question is half the answer is absolutely introductory questions, easy to understand.

But for these three variables L, R, mid, and I was thinking for a long time.

So I have to say about

L is the minimum value satisfying the condition

mid=(L+R)/2;

R is the maximum value satisfying the condition

This question is seeking to make maximum minimum, so of course, is the output R.

The second question Best Cow Fences

On this question there luogu: average

This question is most start to feel really so hard, I thought for a few, are all N ^ 2 * logN, apparently could not pass.

In order to satisfy the segment length because the sub> = k this condition, I use the N ^ 2, so in order to approach a more excellent

! ! Key to the plan below! !

First with the prefix and handle it, and each one must be added to the list before -mid.

Requirement> = k, ① satisfies only once

② long as> = 0 on the line

According ①, we find once break;

According to ②, we simulate the minimum prefix and 1 ~ ik just fine.

So do not simulate the beginning, at least one N

Whole algorithm becomes N * logN

CODE:

#include<bits/stdc++.h>
using namespace std;
int n,m,L,R,ans;
long long s[100005],a[100005],Max;
bool check(){
    long long last=0;
    for(int i=m;i<=n;i++){
        if(s[i]-last>=0) return true;
        last=min(last,s[i-m+1]);
    }
    return false;   
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        a[i]*=1000;
        Max=max(a[i],Max);
    }
    L=0,R=Max;
    long long mid;
    while(L<=R){
        mid=(L+R)/2;    
        for(int i=1;i<=n;i++) s[i]=s[i-1]+a[i]-mid;
        if(check()) L=mid+1;
        else R=mid-1; 
    }
    cout<<R;
    return 0;
}

Guess you like

Origin www.cnblogs.com/fashpoint/p/11330464.html