hdu3507 Print Article(斜率DP优化)
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.
InputThere 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.OutputA single number, meaning the mininum cost to print the article.Sample Input
5 5
5
9
5
7
5
Sample Output
230
这是一道斜率优化的模板题吧。斜率优化算是真的弄懂了个大概,不然第一次听的时候什么也不会。
就是开头就是判断一个条件,不断取出头,保证最优,队列中的就是满足1比2优,2比3优,这样,因为后者进入的时间迟,所以又可以成为最优解,我注释了很多。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring> typedef long long LL;
using namespace std; const int NN=; int n,m;
int dp[NN],sum[NN],q[NN]; int GetY(int i,int j)
{
return sum[i]*sum[i]+dp[i]-(sum[j]*sum[j]+dp[j]);
} int GetX(int i,int j)
{
return *(sum[i]-sum[j]);
} int main()
{
int x;
while(~scanf("%d%d",&n,&m))
{
int head=,tail=;
q[tail++]=;//这一步必须,因为可能前i个数全部作为一段才是最小值
for(int i=;i<=n;i++)
{
scanf("%d",&x);
sum[i]=sum[i-]+x;
while(head+<tail&&GetY(q[head+],q[head])<=GetX(q[head+],q[head])*sum[i])
head++;//更新最优的点
dp[i]=(sum[i]-sum[q[head]])*(sum[i]-sum[q[head]])+m+dp[q[head]];//计算dp[i]的最小值
while(head+<tail&&GetY(i,q[tail-])*GetX(q[tail-],q[tail-])<=GetY(q[tail-],q[tail-])*GetX(i,q[tail-]))
tail--;//以k,j,i为判断斜率,然后去掉j。
q[tail++]=i;
}
printf("%d\n",dp[n]);
}
}
hdu3507 Print Article(斜率DP优化)的更多相关文章
- HDU3507 Print Article —— 斜率优化DP
题目链接:https://vjudge.net/problem/HDU-3507 Print Article Time Limit: 9000/3000 MS (Java/Others) Mem ...
- hdu3507 Print Article[斜率优化dp入门题]
Print Article Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- HDU3507 Print Article(斜率优化dp)
前几天做多校,知道了这世界上存在dp的优化这样的说法,了解了四边形优化dp,所以今天顺带做一道典型的斜率优化,在百度打斜率优化dp,首先弹出来的就是下面这个网址:http://www.cnblogs. ...
- [hdu3507 Print Article]斜率优化dp入门
题意:需要打印n个正整数,1个数要么单独打印要么和前面一个数一起打印,1次打印1组数的代价为这组数的和的平方加上常数M.求最小代价. 思路:如果令dp[i]为打印前i个数的最小代价,那么有 dp[i] ...
- HDU3507 Print Article (斜率优化DP基础复习)
pid=3507">传送门 大意:打印一篇文章,连续打印一堆字的花费是这一堆的和的平方加上一个常数M. 首先我们写出状态转移方程 :f[i]=f[j]+(sum[i]−sum[j])2 ...
- 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(斜率优化DP)
题目链接:hdu 3507 Print Article 题意: 每个字有一个值,现在让你分成k段打印,每段打印需要消耗的值用那个公式计算,现在让你求最小值 题解: 设dp[i]表示前i个字符需要消耗的 ...
- HDU 3507 Print Article(DP+斜率优化)
Print Article Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- HDU 3507 Print Article 斜率优化
Print Article Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
随机推荐
- C++ 用lambda代替 unique_ptr 的Deleter
C++ 用lambda代替 unique_ptr 的Deleter 代码 #include <iostream> #include <cstdlib> #include < ...
- vue父子组件通信
一.父子组件间通信 vue.js 2.0提供了一个ref 的属性: 可以为子组件指定一个索引id 父组件: <template> <div id='user-login'> & ...
- CentOS 7 更改网卡名为eth0
今天用VBOX安装了CentOS7, 但是安装完之后,发现没有ifconfig命令,在网上搜了一下解决办法,在可以连网状态下,输入下面命令即可 然后就可以用ifconfig命令了. 使用ifconfi ...
- Android学习记录:线程
在Java中,线程的建立方法如下. 新建一个类,接口Runnable,重载 run方法 import javax.swing.JTextField; public class test impleme ...
- error: The requested URL returned error: 401 Unauthorized while accessing
我遇到的其中一个问题. 问题描述: 在git push -u origin master是,提示“error: The requested URL returned error: 401 Unauth ...
- C# XmlDocument操作XML
XML:Extensible Markup Language(可扩展标记语言)的缩写,是用来定义其它语言的一种元语言,其前身是SGML(Standard Generalized Markup Lang ...
- RadioButtonList控件如何取得选中的值
1.需求:我现在页面上有放两个单选控件,现在要通过判断不同的单选控件来对页面上的标签进行显示和隐藏操作 2.控件如下 <asp:RadioButtonList ID=" RepeatD ...
- Rsync for windows
说到电脑,我真是一屋子都是. 从房间到大厅,就已经有5台.这还没包括服务器. 虽然这5台电脑我最常用的也只是2~3台.其他的不是给朋友们来坐的时候打打游戏.就是给妈妈上网看看报纸. 不过我相信很多朋友 ...
- 201521123059 《Java程序设计》第八周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 简单泛型定义: public class Pair<T> ...
- 201521123026 《java程序设计》第七周学习总结
1. 本章学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 Q1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 答: public ...