luogu 2627 mowing the lawn

Title Description

In the town a year ago won the best lawn game, Farm John became lazy, never mowed the lawn. Now, a new round of best lawn game began, Farm John hopes to win again.

However, Farm John lawns very dirty, therefore, Farm John can only let his cows to get the job done. Farm John has N (1 <= N <= 100,000) in a row only cows, numbered 1 ... N. Efficiency of each cow is different, i is the efficiency of dairy cows E_i (0 <= E_i <= 1,000,000,000).

Cows close very familiar with, so if more than K Farm John arrangements only continuous cows, then the cows will go on strike partying :). So now Farm John need your help FJ calculate the maximum efficiency can be obtained, and the program does not exceed the continuous K cows.

Input Format

First line: space-separated two integers N and K

The second N + 1 line: first line i + 1 has an integer E_i

Output Format

The first line: a value representing the maximum efficiency value can be obtained Farm John.

Sample input and output

Input # 1

5 2
1
2
3
4
5

Output # 1

12

analysis

(1) remember to open long long

(2) left and right to open and close access to the tail of the column to use the r - 1

(3) The equation makes it possible modification queue optimization monotone

(4) When the state does not, a large space, it can try to put on each element of the state

(5) Note that the initial state, the boundary conditions. For example at the beginning and not the k, a 0 may be inserted in the queue. So as not to miss the first (since the transfer is dp [q [l]] [0]) which can not contain a first

Code

/***************************
User:Mandy.H.Y
Language:c++
Problem:luogu2627
Algorithm:
Date:2019.8.30 
***************************/

#include<bits/stdc++.h>
#define Max(x,y) ((x) > (y) ? (x) : (y))
#define Min(x,y) ((x) < (y) ? (x) : (y))

using namespace std;

const int maxn = 1e5 + 5;

int n,k,l,r;
long long dp[maxn][3];
long long sum[maxn],q[maxn];
//dp[第i只牛][是否包含自身] 
template<class T>inline void read(T &x){
    x = 0;bool flag = 0;char ch = getchar();
    while( ! isdigit(ch)) flag |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar();
    if(flag) x = -x;
}

template<class T>void putch(const T x){
    if(x > 9) putch(x / 10);
    putchar(x % 10 | 48);
}

template<class T>void put(const T x){
    if(x < 0) putchar('-'),putch(-x);
    else putch(x);
}

void file(){
    freopen("testdata.in","r",stdin);
//    freopen("2627.out","w",stdout);
}

voidReadData () { 
    Read (n-); Read (K); 
    for ( int I = . 1 ; I <= n-; ++ I) { 
        Read (SUM [I]); 
        SUM [I] + = SUM [I- . 1 ]; 
    } 
} 

void Work () { 
    Q [R & lt ++] = 0 ; // mainly in order not to consider the first few k
     // as dp [q [l]] [ 0] - sum [q [ L]] may be less than 0, 
     // so the front may occur even plus, plus 0 so as not to drain the first few
     @ as dp [q [l]] [ 0] itself is not included 
    for ( int I = . 1 ; I <= n-; ++ I) { 
    
        the while (L <R & lt && Q [L] <IK) L ++ ;
        
        dp[i][0] = Max(dp[i-1][0],dp[i-1][1]);
        if(l < r) dp[i][1] = dp[q[l]][0] - sum[q[l]] + sum[i];
        else dp[i][1] = sum[i] - sum[i-1];
        
        while(l < r && dp[q[r-1]][0] - sum[q[r-1]] <= dp[i][0] - sum[i]) r--;
        //注意是r - 1; 
        
        q[r++] = (long long)i;
    }
    
    put(Max(dp[n][0],dp[n][1]));
}

int main(){
//    file();
    readdata();
    work();
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/Mandy-H-Y/p/11433096.html