题目描述:
Print Article
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 2485 Accepted Submission(s): 815
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
![](https://i-blog.csdnimg.cn/blog_migrate/deb59af46e26501d1335903fa890b00f.jpeg)
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
Author
Xnozero
Source
Recommend
zhengfeng
第一道经典斜率dp!!!!
思路;
首先,这是一道dp ,很容易写出状态转移方程:
dp[i]=dp[j]+M+(sum[i]-sum[j])^2;
假设 有 k<j<i 且j决策更优 得到:
dp[j]+(sum[i]-sum[j])^2<dp[k]+(sum[i]-sum[k])^2
化简:
((dp[j]+sum[j]^2-(dp[k]+sum[k]^2))<sum[i]*(sum[j]-sum[k])
设 y[i]=dp[i]+sum[i]^2 ,x[i]=sum[i]
上式等价于 (y[j]-y[k])/(x[j]-x[k])<sum[i];
第一道经典斜率dp!!!!
思路;
首先,这是一道dp ,很容易写出状态转移方程:
dp[i]=dp[j]+M+(sum[i]-sum[j])^2;
假设 有 k<j<i 且j决策更优 得到:
dp[j]+(sum[i]-sum[j])^2<dp[k]+(sum[i]-sum[k])^2
化简:
((dp[j]+sum[j]^2-(dp[k]+sum[k]^2))<sum[i]*(sum[j]-sum[k])
设 y[i]=dp[i]+sum[i]^2 ,x[i]=sum[i]
用g(j,k) 表示上式左端 ;
下面证明 当k<j<i 时若g(j,k) >= g(i,j)那么j不能是最优解
1) : 若g(i,j) < sum[ii] ,那么i比j优
2) : 若g(i,j)>=sum[ii],则g(j,k)>g(i,j)>sum[ii] k比j优
由上我们可以维护这么一个队列 ( 有人称之为凸性) 在队列中 若有k<j<i 则必有g(j,k)<g(i,j)
每次更新的时候,从队头开始查找,直到有其(y[he+1]-y(he])/(x[he+1]-x[he])<sum[i]) 为之 。
以上优化就称之斜率优化:
附上小代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x7777777
#define int long long
using namespace std;
int const nMax = 500010;
int a[nMax];
int dp[nMax];
int sum[nMax];
int n,M;
int q[nMax],he,ta;
int dy(int i,int j){
return dp[i]+sum[i]*sum[i]-dp[j]-sum[j]*sum[j];
}
int dx(int i,int j){
return 2*(sum[i]-sum[j]);
}
void query(int i){
while(he<ta){
if(dy(q[he+1],q[he])<=sum[i]*(dx(q[he+1],q[he])))he++;
else break;
}
dp[i]=dp[q[he]]+(sum[i]-sum[q[he]])*(sum[i]-sum[q[he]])+M;
return ;
}
void insert(int i){
while(he<ta){
int y2=q[ta],y1=q[ta-1];
if(dy(y2,y1)*(dx(i,y2))>=dy(i,y2)*dx(y2,y1))ta--;
else break;
}
q[++ta]=i;
return ;
}
main()
{
while(~scanf("%I64d%I64d",&n,&M)){
sum[0]=0;
for(int i=1;i<=n;i++){
scanf("%I64d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
dp[0]=0;
ta=he=0;
q[0]=0;
int ans=inf;
for(int i=1;i<=n;i++){
query(i);
ans=min(ans,dp[i]);
insert(i);
}
printf("%I64d\n",dp[n]);
}
return 0;
}