HDU3507--Print Article(斜率优化DP)

dp斜率优化 Hdu 3507(Print Article)详细题解 题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3507累加器传送门:http://blog.csdn.net/noiau/article/details/71775000题目:Zero has an old printer that doesn’t work well sometimes. As it is antique, he still lik 阅读详情
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

题意:给出一行数,按从左到右的顺序将这段数分块,每一块的代价为区间和的平方+常数M。求最小代价。

思路:用dp[i]表示到i的最小代价,sum[i]表示前缀和

n^2算法是很好想的,显然是超时的...

假设k<j,考虑从k转移到i和从j转移到i这两种决策

1.dp[i] = dp[k]+(sum[i]-sum[k])^2+M

2.dp[i] = dp[j]+(sum[i]-sum[j])^2+M

如果决策j比k更优,则

dp[j]+sum[i]^2-2*sum[i]*sum[j]+sum[j]^2+M < dp[k]+sum[i]^2-2*sum[i]*sum[k]+sum[k]^2+M

化简一下可得(dp[j]+sum[j]^2-dp[k]-sum[k]^2)/(2*(sum[j]-sum[k]) < sum[i]

令yj = dp[j] + sum[j]^2,xj = 2*sum[j];

可以看出左边是个斜率的表达式,我们这里用g[k,j]表示

于是g[k,j] < sum[i]即选择j比选择k的决策更优.

假如g[k,j] >= g[j,i],那么j永远不会被选择,理由如下:

1.假设g[k,j] < sum[i],那么j比k更优,但是i比j更优

2.假设g[k,j] >= sum[i],那么k比j更优

于是我们想到了一个显然的优化,维护一个斜率上升的栈。每次进来一个新点c之前,从栈顶拿2个元素a,b。如果g[a,b] >= g[b,c]。

那么就把b这个点去掉,循环直到斜率满足g[a,b] < g[b,c].

那...dp[i]怎么求呢...

显然把队列里遍历一遍是肯定能找到解的=_=

但是我们仔细看不等式右边的c[i].在题意中默认是>=0.

假设k<j<i.现在我们假设j比k更优.也就是dp[i] = dp[j]+(sum[i]-sum[j])^2+M.

那么对于一个x>i.dp[x]不可能选择j之间的决策,比如k,为什么呢?

因为sum[x] >= sum[i].如果k的决策比j要更优,则有g[k,j]>sum[x].但这与g[k,j]<=sum[i]不符.

到这里就说得差不多了...如果这题的c[i]可以为负...貌似就必须每次都遍历整个栈了?

感谢http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 500080
#define inf 0x3f3f3f3f
int a[maxn],sum[maxn],dp[maxn];
int q[maxn],M;
bool Judge(int a,int b,int Sum)
{
	if(dp[b]+sum[b]*sum[b]-dp[a]-sum[a]*sum[a] < Sum*(2*sum[b]-2*sum[a]))
		return 1;
	return 0;
}
bool Judge1(int a,int b,int c)
{
	if((dp[b]+sum[b]*sum[b]-dp[a]-sum[a]*sum[a])*(2*(sum[c]-sum[b])) >= ((dp[c]+sum[c]*sum[c]-dp[b]-sum[b]*sum[b])*(2*(sum[b]-sum[a]))))	return 1;
	return 0;
}
int  GetAns(int j,int i)
{
	return (dp[j] + (sum[i]-sum[j])*(sum[i]-sum[j]) + M);
}
inline int min(int a,int b,int c)
{
	if(a>b) a = b;
	if(a>c) a = c;
	return a;
}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int n;
	while(scanf("%d%d",&n,&M)==2)
	{
		for(int i = 1;i <= n;i++)
			scanf("%d",&a[i]);
		sum[0] = dp[0] = 0;
		for(int i = 1;i <= n;i++)
			sum[i] = sum[i-1] + a[i];
		int first = 0,rear = 0;
		q[rear++] = 0;
		for(int i = 1;i <= n;i++)
		{
			//先确定dp[i]
			while(first+1 < rear && Judge(q[first],q[first+1],sum[i]))
				first++;
			dp[i] = GetAns(q[first],i);
			while(first+1 < rear && Judge1(q[rear-2],q[rear-1],i))
				rear--;
			q[rear++] = i;
		}
		printf("%d\n",dp[n]);
	}
}


HDU 3507 Print Article(斜率优化DP) 题目链接 基础的斜率优化DP 参考代码: #include #include #include #include using namespace std; const int maxn= 500050; int A[maxn] , dp[maxn] ,sum[maxn], M; int myque[maxn],head,tail,N; #define Y(i) (dp[i]+su 阅读详情

相关推荐

SV fork join_none & for循环结合使用

fork join_none与循环联用常见问题解决方案

ZhangShuHuai11的博客 884

【HDU3507】【斜率优化DP】Print Article题解

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

It's Maverick. 508

Geant4 学习笔记(一):实现multi-thread多线程

简单修改实现Geant4多线程

weixin_52417815的博客 6090

HDU-3507 Print Article(斜率优化DP)

题目: 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 tea...

slark_的博客 466

【Qt实战项目】记事本完整项目

效果图如下所示: 全部代码如下: #include “notepad.h” #include int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.showMaximized(); return a.exec(); }

YIZHILIUSHA2020的博客 580

在VScode中配置Python开发环境(详细图片文字结合)

需要用到的软件下载包在官网里找一下,在window下的点击安装就行了;找一个3.8到3.10版本差不多都完善跟的上;

m0_72811193的博客 1603

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

题目链接 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 ...

不二君 260

易灵思Trion FPGA实战:5分钟搞定MIPI硬核配置(附Interface Designer避坑指南)

本文详细介绍了易灵思Trion FPGA中MIPI硬核的快速配置方法,通过Interface Designer工具实现从时钟配置到数据收发的全流程实战。特别针对初次接触Trion系列的设计师,提供了六个关键配置陷阱及解决方案,帮助开发者高效完成嵌入式视觉和高速数据传输项目。

eee77的博客 803

Hdu 3507 Print Article (线性DP)

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 cert

alpc_paul的专栏 958

75、CAN总线收发器共模范围扩展与抗干扰芯片选型

摘要:本文探讨了CAN总线收发器共模范围扩展与抗干扰芯片选型的关键问题。作者通过风电场的实际案例,指出共模电压超标会导致总线故障甚至芯片损坏。文章分析了不同芯片(如SN65HVD1050、TJA1043等)的共模范围特性,并提供了电路设计建议,包括正确使用共模扼流圈和终端电阻。最后强调选型时要结合实际工况,在共模范围、功耗、速度和成本之间取得平衡,普通工业现场建议选择正负36V共模范围的芯片。

2401_88858637的博客 56

深入解析Linux驱动开发中的I2C时序及I2C高频面试题

在 I2C 通信中,主设备和从设备的角色有着明确的定义。主设备是发起通信的设备,它负责控制整个通信过程。主设备可以是微控制器、处理器等具有控制能力的设备。

大模型大数据攻城狮的专栏 2773

斜率优化dp简讲 && HDU 3507 Print Article

Problem DescriptionZero 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

Time_major 3286

【异常】CNB提示The bill authorization scope cannot access the current request.

摘要:CNB API返回403错误,表明当前token的授权范围无法访问目标仓库。主要原因是:1) REPO_SLUG填写错误,导致资源地址不匹配;2) CNB_TOKEN未授权目标仓库;3) 账号账单状态异常。解决步骤:先检查REPO_SLUG是否与CNB平台仓库地址完全一致;再确认CNB_TOKEN是否绑定目标仓库且有读取权限;最后检查账号账单是否正常且覆盖目标资源。完成排查后更新配置即可解决此授权问题。

奶爸的编程之路,也就一周冷个三天 665

HDU 斜率优化dp 3507 Print Article

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 4567    Accepted Submission(s): 1370 Problem Description Zero has an

We Can 1132

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Print Article HDU - 3507 dp 斜率优化

题解 dp求解,f[i]表示前i项的最小代价,s为前缀和。 转移方程f[i] = min(f[i], f[j] + (s[i] - s[j])^2 + M),dp复杂度O(N^2),使用斜率优化。 设f[i]转移从j比k好(k &lt; j &lt; i) 得斜率方程 f[j] + (s[i] - s[j])^2 + M &lt; f[k] + (s[i] - s[k])^2 + M 约去相同项 ...

CaprYang的博客 767

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

HDU3507 Print Article【斜率优化DP】

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 certa...

MaJorieL 254

Hdu 3507 Print Article (DP_斜率优化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3507 题目大意:给定一个长度为n的序列,和一个常数m,我们可以将序列分成随意段,每段的权值为sum(arr[i]) + C(x 解题思路:做完Hdu的2829,然后再看这题,一切变得如此简单,用两种方法解。      状态转移方程为: dp[i] = min(

1377

HDU 3507 Print Article (斜率优化DP)

Print Article Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 13922 Accepted Submission(s): 4317 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique,

Ever_glow的博客 979

HDU 3507 Print Article 斜率优化DP

题目大意:给出一个序列,可以连续出一段序列[l,r],费用为sum[j][i] ^2+M,求输出整个序列的最小费用。 思路:裸DP方程:f[i] = f[j] + (sum[i] - sum[j - 1]) ^ 2 + M,然后整理一下斜率优化 =>   f[j] + sum[j]^2 = 2 * sum[i] * sum[j] - M - f[i] y = f[j] + sum[j

(╯°口°)╯(┴—┴ 727

HDU 3507 [Print Article]DP斜率优化

题目大意 给定一个长度为\(n(n \leqslant 500000)\)的数列,将其分割为连续的若干份,使得 $ \sum ((\sum_{i=j}^kC_i) +M) $ 最小。其中\(C_i\)为序列中的项的值,\(M\)为常数。$ j,k $ 表示在原序列中连续的某一段的起始位置和结束位置。 解题思路 考虑到\(n\)的范围巨大,肯定不能用\(O(n^2)\)的暴力DP,而贪心又...

TIANFENG_123456的博客 151

HDU3507 Print Article —— 斜率优化DP

题目链接:https://vjudge.net/problem/HDU-3507 Print Article Time Limit: 9000/3000 MS (Java/Others)Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 14899Accepted Submission(s...

alince20008的博客 215

hdu 3507 Print Article 斜率优化dp

【题目大意】 零有一个旧打印机,有时不能很好的工作。因为它是古董,所以零仍然喜欢用它来打印物品。但打印机太老了,工作了很长一段时间肯定会磨损,所以零使用成本来评估这个程度。 一天零想打印一篇有n单词的文章,每个单词i都有一个成本ci。此外,零知道打印k个单词在一行将成本为 M是一个常量 现在零想知道打印的最低成本。 0 ≤ n ≤ 500000, 0 ≤ M ≤ 1000稍微思索一番,就

w4149 625

hdu 3507 Print Article —— 斜率优化DP

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3507 设 f[i],则 f[i] = f[j] + (s[i]-s[j])*(s[i]-s[j]) + m 即 f[j] + s[j]*s[j] = 2*s[i]*s[j] + f[i] - s[i]*s[i] - m 于是维护下凸包即可; 写成 double 的 slp 总是不对,把分...

aodan5477的博客 171

hdu 3507 Print Article(dp+斜率优化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3507 题解:显然这题的递推很容易得到 dp[i]=dp[j]+(sum[i]-sum[j])^2+m 显然这样的要两个for肯定超时。这里就要用到斜率优化。 不妨设k<j<i; 显然这里j是最优解所以dp[j]+m+(sum[i]-sum[j])^2<dp[...

awow80285的博客 302

HDU 3507 Print Article【斜率优化dp】

题目描述: 把长度为n的序列{ci}划分成若干个区间,每个区间的贡献为ci的和的平方加上M,求最小贡献 n&lt;=500000 题目分析: 斜率优化模板题,题解网上很多。。这里主要说一下细节 while(head+1&lt;tail&amp;&amp;gety(q[tail-1],q[tail-2])*getx(i,q[tail-1])&gt;gety(i,q[tail-1])*getx(q[t...

Master.Yi的博客 240

Print Article 【HDU - 3507】【DP斜率优化推导】

题目链接   是一道好题了……一直在WA,不知道是哪里写彪了,后来,没想到,竟然是没考虑到前面所有的数都和在一起才是最小值的可能,就举个例子吧: 5 10000000 0 0 0 0 0 ans:10000000 5 100000000 0 2 3 4 5 ans:100000196     但是,我们若是没关注到这个问题的话,初始的值会极其的大,会多出来一个M的值,所以要考虑到初始...

既然弱小,就只顾变强就是了 288
上一篇: 2014ACM-ICPC牡丹江+鞍山总结
下一篇: POJ1160--Post Office(四边形不等式优化)
SCUT_Pein
博客等级 码龄14年 54粉丝 480原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值