HDU 3507 Print Article(DP+斜率优化)
Print Article
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 7960 Accepted Submission(s): 2465
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.
5
9
5
7
5
【思路】
斜率优化。
设f[i],则转移式为f[i]=min{f[j]+(C[i]-C[j])^2+M},1<=j<i
进一步得:f[i]=min{ (f[j]+C[j]^2-2*C[i]*C[j])+(C[i]^2+M) }
设y(j)=f[j]+C[j]^2,a[i]=-*C[i],x(j)=C(j),则f[i]=min{y(j)+2*a[i]*x(j)}+C[i]^2+M
则要求min p=y+2ax , 单调队列维护下凸包。
【代码】
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; const int N = +; struct point { int x,y;
}q[N],now;
int L,R,n,m,C[N],f[N];
int cross(point a,point b,point c) {
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
void read(int& x) {
char c=getchar(); while(!isdigit(c)) c=getchar();
x=; while(isdigit(c)) x=x*+c-'' , c=getchar();
}
int main() {
while(scanf("%d%d",&n,&m)==) {
for(int i=;i<=n;i++)
read(C[i]) , C[i]+=C[i-];
L=R=;
for(int i=;i<=n;i++) {
while(L<R && q[L].y-*C[i]*q[L].x>=q[L+].y-*C[i]*q[L+].x) L++;
now.x=C[i]; //计算xi
now.y=q[L].y-*C[i]*q[L].x+*C[i]*C[i]+m; //计算yi=f[i]+b[i]^2 = min p+a[i]^2+b[i]^2+M
while(L<R && cross(q[R-],q[R],now)<=) R--;
q[++R]=now;
}
printf("%d\n",q[R].y-C[n]*C[n]);
}
return ;
}
HDU 3507 Print Article(DP+斜率优化)的更多相关文章
- HDU 3507 [Print Article]DP斜率优化
题目大意 给定一个长度为\(n(n \leqslant 500000)\)的数列,将其分割为连续的若干份,使得 $ \sum ((\sum_{i=j}^kC_i) +M) $ 最小.其中\(C_i\) ...
- HDU 3507 Print Article(斜率优化DP)
题目链接 题意 : 一篇文章有n个单词,如果每行打印k个单词,那这行的花费是,问你怎么安排能够得到最小花费,输出最小花费. 思路 : 一开始想的简单了以为是背包,后来才知道是斜率优化DP,然后看了网上 ...
- HDU 3507 Print Article(斜率优化)
显然的斜率优化模型 但是单调队列维护斜率单调性的时候出现了莫名的锅orz 代码 #include <cstdio> #include <algorithm> #include ...
- HDU 3507 Print Article(斜率优化推导)
$dp$,斜率优化. 第一次做斜率优化的题目,看了一些题解,自己总结一下. 这题是说有$n$个数字,可以切成任意段,每一段的费用是这一段数字的和平方加上$M$.问最小费用是多少. 设$dp[i]$为$ ...
- hdu 3507 Print Article(斜率优化DP)
题目链接:hdu 3507 Print Article 题意: 每个字有一个值,现在让你分成k段打印,每段打印需要消耗的值用那个公式计算,现在让你求最小值 题解: 设dp[i]表示前i个字符需要消耗的 ...
- DP(斜率优化):HDU 3507 Print Article
Print Article Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- 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 ...
- HDU 3507 Print Article 斜率优化
Print Article Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- 斜率优化板题 HDU 3507 Print Article
题目大意:输出N个数字a[N],输出的时候可以连续的输出,每连续输出一串,它的费用是 "这串数字和的平方加上一个常数M".n<=500000 我们设dp[i]表示输出到i的时 ...
随机推荐
- C#当中的多线程_任务并行库(上)
复习: 第三章内容中我们提到了三种异步编程模型,这里简单复习一下,分别如下 1.APM(异步编程模式):形如Beginxxx,Endxxx. 2.EAP(基于事件的异步编程模式):这个我们在.net中 ...
- Cocos2d-x 学习资料收集
框架源代码: http://code.google.com/p/cocos2d-x/downloads/list 搭建环境 http://blog.csdn.net/ccf19881030/artic ...
- 一个实例明白AutoResetEvent和 ManulResetEvent的用法
先看一段代码: public class WaitHandlerExample { public static AutoResetEvent waitHandler; ...
- 查看当前使用的shell
1.实时查看当前进程中使用的shell种类:推荐 ps | grep $$ | awk '{print $4}' (注:$$表示shell的进程号) 2.最常用的查看shell的命令,但不能实时反映当 ...
- 通过C#去调用C++编写的DLL
这个问题缠了我2个小时才弄出来,其实很简单.当对方提供一个dll给你使用时,你需要去了解这个dll 是由什么语言写的,怎么编译的,看它的编译类型.这样即使在没有头绪时,你可以先尝使用一些比较热门的编译 ...
- ios7--系统自带的向右滑动手势返回上一个界面
当从控制器A push到控制器B,我们返回控制器A,除了使用按钮返回 [self.navigationController pushViewController:Vc animated:YES]; 还 ...
- AngularJS cookie的使用
Javascript使用document.cookie接口来处理简单的文本cookie,但现在大多数现代浏览器都可以使用html5 API了(sessionstorage和localstorage), ...
- View.setTag()的作用
//这个东西在一些需要用到Adapter自定控件显示方式的时候非常有用 //Adapter 有个getView方法,可以使用setTag把查找的view缓存起来方便多次重用 public View g ...
- 原生js判断是否有某个class,如果有就删掉,没有加上
<style> #div1 { width: 100px; height: 100px; position: absolute; } .div1 { background: red; } ...
- POJ 1014 Dividing 多重背包
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63980 Accepted: 16591 Descri ...