参考博文:http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html//讲的真的很好,有个小错误,博客里的num全为sum,像我这种弱渣都听懂了。真心点赞!!!

Print Article

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 7976    Accepted Submission(s): 2471

Problem Description
Zero has an old printer that doesn't work well sometimes. As it is antique, he still like to use it to print articles. But it is too old to work for a long time and it will certainly wear and tear, so Zero use a cost to evaluate this degree.
One day Zero want to print an article which has N words, and each word i has a cost Ci to be printed. Also, Zero know that print k words in one line will cost

M is a const number.
Now Zero want to know the minimum cost in order to arrange the article perfectly.
 
Input
There are many test cases. For each test case, There are two numbers N and M in the first line (0 ≤ n ≤ 500000, 0 ≤ M ≤ 1000). Then, there are N numbers in the next 2 to N + 1 lines. Input are terminated by EOF.
 
Output
A single number, meaning the mininum cost to print the article.
 
Sample Input
5 5
5
9
5
7
5
 
Sample Output
230
 
Author
 
Xnozero
 
Source
解释:    
 
 题解:
     1,用一个单调队列来维护解集。

2,假设队列中从头到尾已经有元素a b c。那么当d要入队的时候,我们维护队列的上凸性质,即如果g[d,c]<g[c,b],那么就将c点删除。直到找到g[d,x]>=g[x,y]为止,并将d点加入在           该位置中。

3,求解时候,从队头开始,如果已有元素a b c,当i点要求解时,如果g[b,a]<sum[i],那么说明b点比a点更优,a点可以排除,于是a出队。最后dp[i]=getDp(q[head])。

  代码:

 #include<cstdio>
#include<cmath>
#include<iostream>
#define maxn 500100 using namespace std; struct get
{
int n,m,sum[maxn],a[maxn],dp[maxn],q[maxn];
int getup(int j,int k){return dp[j]+sum[j]*sum[j]-dp[k]-sum[k]*sum[k];}//分子
int getdown(int j,int k){return *sum[j]-*sum[k];}//分母
int getdp(int i,int j) {return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);}//dp[i]
get()
{
while (scanf("%d%d",&n,&m)==)
{
for(int i=;i<=n;i++) scanf("%d",&sum[i]);
sum[]=dp[]=;
for(int i=;i<=n;i++)sum[i]+=sum[i-];
int t=,w=;
for (int i=;i<=n;i++)
{
while (t+<w&&sum[i]*getdown(q[t+],q[t])>=getup(q[t+],q[t])) t++;//维护队列,删除就点之前所有点
dp[i]=getdp(i,q[t]);
while (t+<w&&getup(i,q[w-])*getdown(q[w-],q[w-])<=getup(q[w-],q[w-])*getdown(i,q[w-])) w--;//维护队列,保证队列具有 上凸性质。
q[w++]=i;
}
printf("%d\n",dp[n]);
}
}
}get;
int main()
{
get;
return ;
}
 顺便吐槽一下,

Presentation Error 这种错误你们见过么?我也是醉了!

Recommend
zhengfeng   |   We have carefully selected several similar problems for you:  3506 3501 3504 3505 3498 
 

c++之路进阶——hdu3507(Print Article)的更多相关文章

  1. HDU3507 Print Article —— 斜率优化DP

    题目链接:https://vjudge.net/problem/HDU-3507 Print Article Time Limit: 9000/3000 MS (Java/Others)    Mem ...

  2. hdu3507 Print Article

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) P ...

  3. hdu3507 Print Article[斜率优化dp入门题]

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  4. hdu3507 Print Article(斜率DP优化)

    Zero has an old printer that doesn't work well sometimes. As it is antique, he still like to use it ...

  5. hdu3507 Print Article(斜率优化入门)(pascal)

    Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique, he s ...

  6. HDU3507 Print Article(斜率优化dp)

    前几天做多校,知道了这世界上存在dp的优化这样的说法,了解了四边形优化dp,所以今天顺带做一道典型的斜率优化,在百度打斜率优化dp,首先弹出来的就是下面这个网址:http://www.cnblogs. ...

  7. HDU3507 Print Article (斜率优化DP基础复习)

    pid=3507">传送门 大意:打印一篇文章,连续打印一堆字的花费是这一堆的和的平方加上一个常数M. 首先我们写出状态转移方程 :f[i]=f[j]+(sum[i]−sum[j])2 ...

  8. HDU-3507 Print Article (斜率优化)

    题目大意:将n个数分成若干个区间,每个区间的代价为区间和的平方加上一个常数m,求最小代价. 题目分析:定义状态dp(i)表示前 i 个数已经分好的最小代价,则状态转移方程为 dp(i)=min(dp( ...

  9. 2018.08.29 hdu3507 Print Article(斜率优化dp)

    传送门 这应该算是斜率优化的模板题了. 就是要求打印n个数,每个数有一个参数a[i],每打印一段连续的数[l,r]需要的花费是(a[[l]+...+a[r])2+m" role=" ...

随机推荐

  1. 图片放大方法、、菜单栏的位置随滚轮移动固定方法、、<a></a>去外层虚线方法:a:focus { outline:none; -moz-outline:none;};

    图片放大方法一: <style type="text/css">.xt{ width:230px; height:230px;}.tp{ width:230px; he ...

  2. 单选按钮控件(Ridio Button)的使用

    VC学习笔记5:单选按钮控件(Ridio Button)的使用 一.对单选按钮进行分组: 每组的第一个单选按钮设置属性:Group,Tabstop,Auto;其余按钮设置属性Tabstop,Auto. ...

  3. final关键字+const关键字

    final关键字 1.如果我们希望某个类不被其它的类来继承(可能因为安全考虑),可以使用final. 例题 <? final class A{} class B extends A{};//会报 ...

  4. 蓝牙的SDP协议总结

    1.概念     SDP协议让客户机的应用程序发现存在的服务器应用程序提供的服务以及这些服务的属性.SDP只提供发现服务的机制,不提供使用这些服务的方法.每个蓝牙设备都需要一个SDP Service, ...

  5. UITextView 监听 return key的改变

    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSSt ...

  6. php 显示某一个时间相当于当前时间在多少秒前,多少分钟前,多少小时前

    /* *function:显示某一个时间相当于当前时间在多少秒前,多少分钟前,多少小时前 *timeInt:unix time时间戳 *format:时间显示格式 */ public function ...

  7. C#对象的声明与实例化

    在代码中我们经常能看到下面这样的语句. A a = new A();(1) B b = null;(2) C c;(3) 代码( 1 ) 创建 A 的对象并对其进行初始化. A :类: new A() ...

  8. Android系统框架

    从上图中可以看出,Android系统架构为四层结构,从上层到下层分别是应用程序层.应用程序框架层.系统运行库层以及Linux内核层 应用程序层: 简单说就是我们用户肉眼能看到的应用程序,如SMS短信客 ...

  9. 实现简易的android 直播技术

    Android 的直播,主要使用的是camera采集的数据推流到服务器上,在客户端播放camera采集的数据.采用SurfaceView+ SurfaceTexture来显示camera数据, Sur ...

  10. Android 拉伸四周"空白",中间内容不变的9Patch

    系统自带tools下的9Patch制作工具: 在左边,画线的视图里,Zoom到最大,分别在上方和左方的四个端点,各点一下,效果: 看看这四个点的位置: 二.补充一个箭头尾巴可自由拉伸的9patch示例 ...