HDU3507
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
Author
Xnozero
Source
2010 ACM-ICPC Multi-University Training Contest(7)——Host by HIT
Recommend
zhengfeng | We have carefully selected several similar problems for you: 3506 3501 3504 3505 3498
本题是标准的斜率优化,我从此题入门。首先先假设对于j
//new experience get!that's:函数调用非常非常慢!!!!!!!
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define F(i,a,b) for(int i=a;i<=b;i++)
#define r(x) scanf("%d",&x)
#define p(x) printf("%d",x)
#define pk(x) printf("%d ",x)
#define pln(x) printf("%d\n",x)
#define ln puts("")
#define N 500005
#define LL long long
int q[N*2],h,t;
LL S[N],X[N];
int m,n;
LL DOWN(int x,int y)
{
return 2*S[x]-2*S[y];
}
LL UP(int x,int y)
{
return((X[x]+S[x]*S[x])-(X[y]+S[y]*S[y]));
}
LL WHAT_DOES_NOT_KILL_YOU_MAKES_YOU_STRONGER(int x,int y)
{
return (S[x]-S[y])*(S[x]-S[y])+m+X[x];
}
int main()
{
while(scanf("%d%d",&n,&m)==2)
{
F(i,1,n) r(S[i]);
F(i,1,n) S[i]+=S[i-1];
h=t=0;
q[++t]=0;
F(i,1,n)
{
while(t>h&&UP(q[h+1],q[h])<=S[i]*DOWN(q[h+1],q[h])) h++;
X[i]=WHAT_DOES_NOT_KILL_YOU_MAKES_YOU_STRONGER(q[h],i);
while(t>h&&UP(i,q[t])*DOWN(q[t],q[t-1])<=UP(q[t],q[t-1])*DOWN(i,q[t]))t--;
q[++t]=i;
}
cout<<X[n];
ln;
}
}