Luogu1714 切蛋糕

Posted zcdhj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Luogu1714 切蛋糕相关的知识,希望对你有一定的参考价值。

Luogu

单调队列板子题。

很明显\[Ans=\max_{1≤i≤n} \{\sum_{j=1}^i P_i-\min_{0≤k<i} \{\sum_{j=1}^k P_j\} \}\]

单调队列维护即可。

#include <iostream>
#include <cstdio>
#include <queue> 

const int max_n = 500000 + 5;
const int inf = 0x7f7f7f7f;

int N, M, Ans = -inf;
int sum[max_n];

std::deque <int> q;

inline int read()
{
    register int x = 0, v = 1;
    register char ch = getchar();
    while(!isdigit(ch)) 
    {
        if(ch == ‘-‘) v = -1;
        ch = getchar();
    }
    while(isdigit(ch))
    {
        x = (x << 1) + (x << 3) + ch - ‘0‘;
        ch = getchar();
    }
    return x * v;
} 

int main()
{
    N = read();
    M = read();
    for(int i = 1; i <= N; ++i) sum[i] = sum[i - 1] + read();
    for(int i = 1; i <= N; ++i)
    {
        while(!q.empty() && sum[q.back()] > sum[i]) q.pop_back();
        q.push_back(i);
        while(!q.empty() && i - q.front() > M) q.pop_front();
        Ans = std::max(Ans, sum[i] - sum[q.front()]); 
    }
    printf("%d\n", Ans);
    return 0;
}

以上是关于Luogu1714 切蛋糕的主要内容,如果未能解决你的问题,请参考以下文章

Luogu P1714切蛋糕(面向对象编程首次尝试?)

洛谷—— P1714 切蛋糕

P1714 切蛋糕

单调队列P1714 切蛋糕

P1714切蛋糕(不定区间最值)

[洛谷P1714]切蛋糕