POJ3612Telephone Wire题解动态规划DP

Telephone Wire
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1735 Accepted: 596

Description

Farmer John's cows are getting restless about their poor telephone service; they want FJ to replace the old telephone wire with new, more efficient wire. The new wiring will utilize N (2 ≤ N ≤ 100,000) already-installed telephone poles, each with some heighti meters (1 ≤ heighti ≤ 100). The new wire will connect the tops of each pair of adjacent poles and will incur a penalty cost C × the two poles' height difference for each section of wire where the poles are of different heights (1 ≤ C ≤ 100). The poles, of course, are in a certain sequence and can not be moved.

Farmer John figures that if he makes some poles taller he can reduce his penalties, though with some other additional cost. He can add an integer X number of meters to a pole at a cost of X2.

Help Farmer John determine the cheapest combination of growing pole heights and connecting wire so that the cows can get their new and improved service.

Input

* Line 1: Two space-separated integers: N and C
* Lines 2..N+1: Line i+1 contains a single integer: heighti

Output

* Line 1: The minimum total amount of money that it will cost Farmer John to attach the new telephone wire.

Sample Input

5 2
2
3
5
1
4

Sample Output

15

Source

状态:
d[i][j]表示第i个高度为j时最小花费
状态转移方程:
d[i][j]=min(d[i][j],d[i-1][k]+abs(j-k)*m+(a[i]-j)*(a[i]-j));
这样肯定超时,不知道怎么优化
看了别人的解题报告:
/*
DP
设f[i][j]表示第i个电线杆高度为j时的最小花费,
DP方程为f[i][j]=min( (j-a[i])^2+f[i-1][k]+|j-k|*c )
直接根据这条方程做的话复杂度是100000*100*100,明显会超时.
可以将方程进行化简:
f[i][j]=min( (j-a[i])^2+f[i-1][k]+|j-k|*c ) (j>=a[i])
       =(j-a[i])^2+j*c+min(f[i-1][k]-k*c) (j>=k)
     (j-a[i])^2-j*c+min(f[i-1][k]+k*c) (j<k)
设high[j]=min(f[i-1][k]-k*c) (j>=k)
low[j]=min(f[i-1][k]+k*c) (j<k)
方程就变为f[i][j]=(j-a[i])^2+min(high[j]+j*c,low[j]-j*c);
可以通过预处理计算先出high[j],low[j].
*/

 

代码:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值