POJ3017
2024-08-31 14:52:28
题意
将一段序列分割为任意段,每一段的连续和不超过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 ;
}
最新文章
- spring+springmvc+mybatis xml配置文件
- MFC的本质
- 开始使用MarkDown写博客
- when will a databasechange be committed?
- 解决org.openqa.selenium.WebDriverException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms org.springframework.beans.BeanInstantiation
- NPM小结
- latex数字加粗后变宽
- 【Matplotlib】 标注摄氏度符号
- 任意阶魔方阵(幻方)的算法及C语言实现
- No.006 ZigZag Conversion
- JDK 1.6 下载 地址
- Apache HTTP Server安装教程
- Oracle“死锁”模拟
- Codevs 1217 借教室 2012年NOIP全国联赛提高组
- BZOJ1631: [Usaco2007 Feb]Cow Party
- HTML中元素水平居中。
- [每日一题] OCP1z0-047 :2013-08-29 NULL............................................................168
- adapter pattern
- 弹指之间 -- Folk Rock
- bzoj 1188