题意

将一段序列分割为任意段,每一段的连续和不超过M,使得每一段最大值的和最小.

分析

用单调队列进行优化的dp。单调队列可以维护可以影响当前区间的最大值。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int maxn=+;
int a[maxn];
long long f[maxn];
long long sum[maxn];
int n;
long long m;
int main(){
scanf("%d%lld",&n,&m);
sum[]=;
int ok=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
if(a[i]>m)ok=;
}
if(!ok){printf("-1");return ;}
f[]=;
f[]=a[];
deque<int>q;
int beg=;
for(int i=;i<=n;i++){
while(!q.empty()&&a[i]>=a[q.back()])q.pop_back();
while(sum[i]-sum[beg-]>m&&beg<i)beg++;
q.push_back(i);
while(q.front()<beg&&!q.empty())q.pop_front();
f[i]=f[beg-]+a[q.front()];
for(int k=;k<=q.size();k++){
int b=q.front();
q.pop_front();
if(!q.empty())
f[i]=min(f[i],f[b]+a[q.front()]);
q.push_back(b);
}
}
printf("%lld",f[n]);
return ;
}

最新文章

  1. spring+springmvc+mybatis xml配置文件
  2. MFC的本质
  3. 开始使用MarkDown写博客
  4. when will a databasechange be committed?
  5. 解决org.openqa.selenium.WebDriverException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms org.springframework.beans.BeanInstantiation
  6. NPM小结
  7. latex数字加粗后变宽
  8. 【Matplotlib】 标注摄氏度符号
  9. 任意阶魔方阵(幻方)的算法及C语言实现
  10. No.006 ZigZag Conversion
  11. JDK 1.6 下载 地址
  12. Apache HTTP Server安装教程
  13. Oracle“死锁”模拟
  14. Codevs 1217 借教室 2012年NOIP全国联赛提高组
  15. BZOJ1631: [Usaco2007 Feb]Cow Party
  16. HTML中元素水平居中。
  17. [每日一题] OCP1z0-047 :2013-08-29 NULL............................................................168
  18. adapter pattern
  19. 弹指之间 -- Folk Rock
  20. bzoj 1188

热门文章

  1. jQuery attr 与 prop 区别最简单分析
  2. Zookeeper在Dubbo中的作用及Zk集群的选举原理
  3. 使用 event.preventDefault 拦截表单的提交
  4. Elasticsearch.net项目
  5. Python笔记-2
  6. js中typeof用法详细介绍
  7. 系列文章--SQLite文章
  8. 【精品分享二】ASP.NET MVC系列精品图书高清PDF下载
  9. el表达式的坑
  10. 【转】理解JMeter聚合报告(Aggregate Report)