码迷,mamicode.com
首页 > 其他好文 > 详细

Luogu1714 切蛋糕

时间:2018-02-22 10:45:23      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:==   front   main   ack   isp   ret   include   queue   单调队列   

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 切蛋糕

标签:==   front   main   ack   isp   ret   include   queue   单调队列   

原文地址:https://www.cnblogs.com/zcdhj/p/8457905.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!