Print Article

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

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
 
  学到了,这样的斜率优化。
  

 #include <iostream>
#include <cstdio>
using namespace std;
const int maxn=;
long long f[maxn],s[maxn];
int q[maxn];
int main()
{
int n,m,front,back;
while(~scanf("%d%d",&n,&m))
{
s[]=f[]=;
for(int i=;i<=n;i++)scanf("%d",&s[i]);
for(int i=;i<=n;i++)s[i]+=s[i-]; front=;back=;
q[front]=; for(int i=;i<=n;i++)
{
while(front<back-&&(f[q[front+]]+s[q[front+]]*s[q[front+]])-(f[q[front]]+s[q[front]]*s[q[front]])<=*s[i]*(s[q[front+]]-s[q[front]])) front++; f[i]=f[q[front]]+(s[i]-s[q[front]])*(s[i]-s[q[front]])+m; while(front<back-&&(s[q[back-]]-s[q[back-]])*((f[i]+s[i]*s[i])-(f[q[back-]]+s[q[back-]]*s[q[back-]]))<=(s[i]-s[q[back-]])*((f[q[back-]]+s[q[back-]]*s[q[back-]])-(f[q[back-]]+s[q[back-]]*s[q[back-]])))back--;
q[back++]=i;
}
printf("%lld\n",f[n]);
}
return ;
}
 

DP(斜率优化):HDU 3507 Print Article的更多相关文章

  1. hdu 3507 Print Article(斜率优化DP)

    题目链接:hdu 3507 Print Article 题意: 每个字有一个值,现在让你分成k段打印,每段打印需要消耗的值用那个公式计算,现在让你求最小值 题解: 设dp[i]表示前i个字符需要消耗的 ...

  2. HDU 3507 [Print Article]DP斜率优化

    题目大意 给定一个长度为\(n(n \leqslant 500000)\)的数列,将其分割为连续的若干份,使得 $ \sum ((\sum_{i=j}^kC_i) +M) $ 最小.其中\(C_i\) ...

  3. HDU 3507 Print Article(DP+斜率优化)

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

  4. HDU 3507 Print Article(斜率优化DP)

    题目链接 题意 : 一篇文章有n个单词,如果每行打印k个单词,那这行的花费是,问你怎么安排能够得到最小花费,输出最小花费. 思路 : 一开始想的简单了以为是背包,后来才知道是斜率优化DP,然后看了网上 ...

  5. HDU 3507 - Print Article - [斜率DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3507 Zero has an old printer that doesn't work well s ...

  6. HDU 3507 Print Article 斜率优化

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

  7. HDU 3507 Print Article(斜率优化)

    显然的斜率优化模型 但是单调队列维护斜率单调性的时候出现了莫名的锅orz 代码 #include <cstdio> #include <algorithm> #include ...

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

    $dp$,斜率优化. 第一次做斜率优化的题目,看了一些题解,自己总结一下. 这题是说有$n$个数字,可以切成任意段,每一段的费用是这一段数字的和平方加上$M$.问最小费用是多少. 设$dp[i]$为$ ...

  9. 斜率优化板题 HDU 3507 Print Article

    题目大意:输出N个数字a[N],输出的时候可以连续的输出,每连续输出一串,它的费用是 "这串数字和的平方加上一个常数M".n<=500000 我们设dp[i]表示输出到i的时 ...

随机推荐

  1. Python之路【第十篇】:HTML -暂无等待更新

    Python之路[第十篇]:HTML -暂无等待更新

  2. tj

    --统计set @collSql='select sum(case Ca_IssueType when 0 then 1 else 0 end) as IssueCount,sum(case when ...

  3. sql语句中like的使用

    先看一道题: 写出一条sql语句,找出表B中 字段Value中不全是字母 数字 下划线的数据 初看这道题,我们想到可以用like去进行模糊匹配,找出想要的结果.但是有一个地方需要注意:如果想在SQL ...

  4. [转]关于java中的 sychronized 同步方法 与 同步块的理解

    首先,需要说明一点,也是最重要的一点,无论是同步方法 还是 同步块 都是只针对同一个对象的多线程而言的,只有同一个对象产生的多线程,才会考虑到 同步方法 或者是 同步块,如果定义多个实例的同步,可以考 ...

  5. delegate-使用笔记

    public class testclass { public class ProductImages : Page { protected Repeater rptSmallUrls; protec ...

  6. topcoder算法练习2

    Problem Statement      In most states, gamblers can choose from a wide variety of different lottery ...

  7. Linux 系统命令及其使用详解(大全)

    (来源: 中国系统分析员) cat cd chmod chown cp cut 1.名称:cat 使用权限:所有使用者 使用方式:cat [-AbeEnstTuv] [--help] [--versi ...

  8. maven 常用插件总结

    maven-javadoc-plugin (1) 说明:该插件生成项目的javadoc.对于构建jar目标,javadoc会首先生成并打包放入jar文件中. (2) 默认用法: pom.xml配置 & ...

  9. WPF自定义DataGrid分页控件

    新建Custom Control,名:PagingDataGrid 打开工程下面的Themes\Generic.xaml xaml里面代码替换如下 <Style x:Key="{x:T ...

  10. Apache:To Config The Vhost of Django Project

    It is not a good idea to use dev server in Production Environment. Apache or Nginx are good choice.B ...