loj10176. 「一本通 5.5 例 2」最大连续和

思路:

  单调队列优化dp,注意最后统计答案时要初始化ans为负无穷。

#include
#include
#include
using namespace std;
const int maxn = 200010;
inline void qread(int &x){
    x = 0;    register int ch = getchar(), flag = 0;
    while(ch < '0' || ch > '9')    {if(ch == '-') flag = 1; ch = getchar();}
    while(ch >= '0' && ch <= '9')    x = 10 * x + ch - 48, ch = getchar();
    if(flag)    x = -x;
}
int pos[maxn], qu[maxn];
int head = 1, tail, ans;
int sum[maxn];
int    data[maxn];
int dp[maxn];
int n, m;
void dpmin(){
    for(int i = 1; i <= n; ++i){
        while(pos[head] < i - m + 1 && head <= tail)    head++;
        while(qu[tail] >= sum[i - 1] && head <= tail)        tail--;
        pos[++tail] = i;    qu[tail] = sum[i - 1];    dp[i] = sum[i] - qu[head];    
    }
}
int main(void){
    cin >> n >> m;
    for(int i = 1; i <= n; ++i)
        qread(data[i]);
    for(int i = 1; i <= n; ++i)
        sum[i] = sum[i - 1] + data[i];
    dpmin();
    ans = 0x80000000;
    for(int i = 1; i <= n; ++i)
        ans = max(dp[i], ans);
    cout << ans << endl;    
    return 0;
}

 

转载于:https://www.cnblogs.com/junk-yao-blog/p/9504359.html

你可能感兴趣的:(loj10176. 「一本通 5.5 例 2」最大连续和)