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. PowerDesigner15的安装和破解

    一.PowerDesigner15的安装 运行安装包,出现如下安装界面

  2. jdk配置及maven配置

    jdk配置及maven配置 >>>>>>>>>>>>>>>>>>>>>&g ...

  3. asp.net mvc4 使用java异步提交form表单时出现[object object] has no method ajaxSubmit

    最近接手了一个单子,说大不大,只是功能不少,开发过程中遇到该问题 先看脚本截图: 本以为是笔误,哪儿写错了,可是看来看去,都没发现有不合适的地方,对比过网上很多代码,都差不多,于是各种方式的,各种原因 ...

  4. 02.[WPF]如何固定窗口的大小

    在WPF开发过程中碰到一个需求,要求保证窗口大小不变,即便是双击 titlebar 也不能改变窗口大小和位置.要实现这样的效果,需要执行如下步骤: 1,分别设置窗口的 Width/MaxWidth/M ...

  5. Spot light工具集

    Spot light on UNIX 安装没什么问题 Spot light on Oracle  必须安装32位的客户端,不然搞死你 两者的界面都是吊炸天啊

  6. 强大的Core Image框架,各种滤镜处理图像

    首先介绍一下Core Image,他是一个很强大的图像处理框架,他可以让你简单的应用各种滤镜来处理图像,比如说色相,饱和度,亮度等等...他是运用GPU(CPU)实时地处理图像数据和视频的帧.而且Co ...

  7. UITableViewCell 左滑删除

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return Y ...

  8. this point

    // this.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespa ...

  9. Java学习----一个对象怎么调用另一个对象呢?

    打包 jar cvfm test1.jar manifmest.mf teacher.class student.class test1.class 运行 java -jar test1.jar pu ...

  10. gdb小结

    testGdb.c #include<stdio.h> int getSum(int a,int b){ printf("a+b=%d\n",a+b); return ...