Cut the Sequence
Time Limit: 2000MS   Memory Limit: 131072K
Total Submissions: 8764   Accepted: 2576

Description

Given an integer sequence { an } of length N, you are to cut the sequence into several parts every one of which is a consecutive subsequence of the original sequence. Every part must satisfy that the sum of the integers in the
part is not greater than a given integer M. You are to find a cutting that minimizes the sum of the maximum integer of each part.

Input

The first line of input contains two integer N (0 < N ≤ 100 000),
M. The following line contains N integers describes the integer sequence. Every integer in the sequence is between 0 and 1 000 000 inclusively.

Output

Output one integer which is the minimum sum of the maximum integer of each part. If no such cuttings exist, output −1.

Sample Input

8 17
2 2 2 8 1 8 2 1

Sample Output

12

把序列分成若干部分,每一部分的和不超过m。求每一部分里最大值和的最小值。

開始没啥思路,研究了半天,感觉单调队列dp很的精妙,先mark一下,后面慢慢理解吧。

代码:

/* ***********************************************
Author :_rabbit
Created Time :2014/5/13 1:35:25
File Name :C.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
ll que[100100],a[100100],dp[100100];
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//dp 方程:f[i]=f[j]+max(x[j+1],x[j+2],...,x[i]),当中j<i,x[j+1]+x[j+2]+...+x[i]<=m;
ll n,m;
while(~scanf("%lld%lld",&n,&m)){
bool flag=1;
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
if(a[i]>m)flag=0;
}
if(!flag){
puts("-1");continue;
}
ll front=0,rear=0,p=1;
dp[1]=a[1];que[rear++]=1;
ll sum=a[1];
for(ll i=2;i<=n;i++){
sum+=a[i];
while(sum>m)sum-=a[p++];//区间和小于等于m
while(front<rear&&a[i]>=a[que[rear-1]])rear--;//单调严格递减队列
que[rear++]=i;
while(que[front]<p&&front<rear)front++;//把远离的弹出。
dp[i]=dp[p-1]+a[que[front]];
for(ll j=front+1;j<rear;j++)
dp[i]=min(dp[i],dp[que[j-1]]+a[que[j]]);//枚举队列中的元素,求最优解。
}
cout<<dp[n]<<endl;
}
return 0;
}

POJ 3017 单调队列dp的更多相关文章

  1. poj 3017 单调队列优化动态规划

    思路:dp[i]=min{dp[j]+max(num[j+1]...num[i])},其中sum[i]-sum[j]<=m. 那么我们需要用单调队列维护j到i的最大值. #include< ...

  2. POJ 1821 单调队列+dp

    题目大意:有K个工人,有n个墙,现在要给墙涂色.然后每个工人坐在Si上,他能刷的最大范围是Li,且必须是一个连续子区间,而且必须过Si,他刷完后能获得Pi钱 思路:定义dp[i][j]表示前i个人,涂 ...

  3. [TyvjP1313] [NOIP2010初赛]烽火传递(单调队列 + DP)

    传送门 就是个单调队列+DP嘛. ——代码 #include <cstdio> ; , t = , ans = ~( << ); int q[MAXN], a[MAXN], f ...

  4. zstu 4237 马里奥的求救——(单调队列DP)

    题目链接:http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4237 这题可以转化为每次可以走g~d+x步,求最大分数,且最大分数的步数最少. ...

  5. 1304F2 - Animal Observation (hard version) 线段树or单调队列 +DP

    1304F2 - Animal Observation (hard version) 线段树or单调队列 +DP 题意 用摄像机观察动物,有两个摄像机,一个可以放在奇数天,一个可以放在偶数天.摄像机在 ...

  6. Sliding Window POJ - 2823 单调队列模板题

    Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...

  7. POJ 2373 单调队列优化DP

    题意: 思路: f[i] = min(f[j]) + 1; 2 * a <= i - j <= 2 *b: i表示当前在第i个点.f[i]表示当前最少的线段个数 先是N^2的朴素DP(果断 ...

  8. POJ - 1821 单调队列优化DP + 部分笔记

    题意:n个墙壁m个粉刷匠,每个墙壁至多能被刷一次,每个粉刷匠要么不刷,要么就粉刷包含第Si块的长度不超过Li的连续墙壁(中间可不刷),每一块被刷的墙壁都可获得Pi的利润,求最大利润 避免重复粉刷: 首 ...

  9. POJ 2838 单调队列

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 55309   Accepted: 15911 ...

随机推荐

  1. 设计模式(八)装饰器模式Decorator(结构型)

    设计模式(八)装饰器模式Decorator(结构型) 1. 概述 若你从事过面向对象开发,实现给一个类或对象增加行为,使用继承机制,这是所有面向对象语言的一个基本特性.如果已经存在的一个类缺少某些方法 ...

  2. 在SQL 语句批量替换数据库字符串的方法

    update table[表名] set Fields[字段名]=replace(Fields[字段名],'被替换原内容','要替换成的内容')update ProgInfo set JoinTime ...

  3. 通过sharedpreference两个程序共享数据

    一.整体工程图      二.SharePreferenceWriteActivity.java package org.ourunix.android.sharepreferencewrite; i ...

  4. 理光C5502A 打印模糊问题

    1.这款打印机好几W,我来的时候就有了.挺高端的. 2.来的时候由于网络没建成.建成之后,全部设置成网络打印机. 3.可以扫描成jpg\pdf,并且可以通过共享设置成扫描到目的地. 4.还有其它一些功 ...

  5. C/C++ 内存布局详解(经典)(很久前不知哪儿转载的)

    一个由C/C++编译的程序除了存放函数二进制代码的程序代码段(code段)外,数据占用的内存大致分为以下几个部分: 1.栈区(stack) 存放局部变量.函数参数.返回数据.返回地址等.系统自动分配释 ...

  6. Qt 无边框窗体改变大小 完美实现

    近期,做项目用到无边框窗体,令人蛋疼的是无边框窗体大小的改变要像右边框那样,上下左右四周,而且要流畅. 网上也找了些代码,发现居然还要连接到windows事件,这显然不合常理,后来自己新建了demo, ...

  7. StrPos,StrScan,

    Delphi提供的字符串函数里有一个Pos函数,它的定义是: function Pos(Substr: string; S: string): Integer; 它的作用是在字符串S中查找字符串Sub ...

  8. 黑龙江省第七届大学生程序设计竞赛-Mysterious Organization

    描述 GFW had intercepted billions of illegal links successfully. It has much more effect. Today, GFW i ...

  9. Activity的绘制流程简单分析(基于android 4.0源码进行分析)

    要明白这个流程,我们还得从第一部开始,大家都知道 在activity里面 setcontentview 调用结束以后 就可以看到程序加载好我们的布局文件了,从而让我们在手机上看到这个画面. 那么我们来 ...

  10. C#中对文件的操作

    详细介绍参考:http://blog.csdn.net/wangyue4/article/details/4616801 源码举例: public class FileSystemManager { ...