hdu 3507 斜率優化DP入門題
http://acm.hdu.edu.cn/showproblem.php?pid=3507
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.
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
/**
hdu3507 斜率優化dp
膜拜大神:http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int N=500005;
int dp[N];
int q[N];
int sum[N];
int head,tail,n,m;
int get_dp(int i,int j)
{
return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);
}
int get_up(int j,int k)
{
return dp[j]+sum[j]*sum[j]-(dp[k]+sum[k]*sum[k]);
}
int get_down(int j,int k)
{
return 2*sum[j]-2*sum[k];
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=1; i<=n; i++)
scanf("%d",&sum[i]);
sum[0]=dp[0]=0;
for(int i=1; i<=n; i++)
sum[i]+=sum[i-1];
head=tail=0;
q[tail++]=0;
for(int i=1; i<=n; i++)
{
while(head+1<tail&&get_up(q[head+1],q[head])<=sum[i]*get_down(q[head+1],q[head]))
head++;
dp[i]=get_dp(i,q[head]);
while(head+1<tail&&get_up(i,q[tail-1])*get_down(q[tail-1],q[tail-2])<=get_up(q[tail-1],q[tail-2])*get_down(i,q[tail-1]))
tail--;
q[tail++]=i;
}
printf("%d\n",dp[n]);
}
return 0;
}
相關文章
- 【DP】斜率優化初步優化
- 斜率優化(凸包優化)DP問題acm優化ACM
- 斜率優化DP總結優化
- 斜率最佳化 DP
- 斜率最佳化dp
- poj 1180 dp的斜率優化優化
- 洛谷P2365/5785 任務安排 題解 斜率優化DP優化
- 淺談斜率最佳化DP
- bzoj3156: 防禦準備(斜率優化+Dp)優化
- HDU 4427 Math Magic【dp+優化+滾動陣列】【好題】優化陣列
- 概率DP入門題
- BZOJ 1597 [Usaco2008 Mar]土地購買:斜率優化dp優化
- 斜率優化動態規劃優化動態規劃
- 斜率優化學習筆記優化筆記
- 【DP】區間DP入門
- BZOJ 1010 [HNOI2008]玩具裝箱toy:斜率優化dp優化
- BZOJ 1096 [ZJOI2007]倉庫建設:斜率優化dp優化
- bzoj1911: [Apio2010]特別行動隊(斜率優化+Dp)API優化
- HDU 2594 (KMP入門)KMP
- BZOJ 1492 [NOI2007]貨幣兌換Cash:斜率優化dp + cdq分治優化
- HDU 1556 Color the ball 線段樹入門題
- 斜率最佳化入門
- bzoj1597: [Usaco2008 Mar]土地購買(斜率優化+Dp+單調佇列)優化佇列
- Webpack入門以及打包優化Web優化
- tableView入門到效能優化View優化
- HDU 3853 LOOPS(概率dp)OOP
- 簡單的揹包問題(入門)HDU2602 HDU2546 HDU1864
- HDU7458-啟發式合併最佳化DP
- 狀壓DP基礎入門
- 洛谷P4027 [NOI2007]貨幣兌換(dp 斜率優化 cdq 二分)優化
- HDU 4055 Number String:字首和優化dp【增長趨勢——處理重複選數】優化
- HDU 4669 Mutiples on a circle (DP , 統計)
- 斜率最佳化(李超樹)
- 斜率最佳化筆記筆記
- Apache Kylin 入門 6 - 優化 CubeApache優化
- 【記憶優化搜尋/dp】HDU - 6415 - 杭電多校第九場 - Rikka with Nash Equilibrium優化UI
- 【基礎dp】HDU 1260 Tickets
- hdu 3401 單調佇列+DP佇列