hdu3507Print Article【斜率优化入门题】


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
hdu3507Print Article【斜率优化入门题】_第1张图片
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
 

菜到不能再菜,从上午卡脑残问题到现在神志不清醒==

参考博客:点击打开链接 点击打开链接 点击打开链接

等下一个题再说吧

/*****************
hdu3507
2016.2.24
358MS	7524K	1005 B	C++
*****************/
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int q[500005],f[500005],s[500005],n,m;
bool cal(int ax,int ay,int bx,int by,int cx,int cy)
{
    return (ax-bx)*(cy-by)>=(ay-by)*(cx-bx);
}
int getx(int x)
{
    return s[x-1];
}
int gety(int x)
{
    return f[x-1]+s[x-1]*s[x-1];
}
int get(int i,int a)
{
    return gety(i)-2*a*getx(i);
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;i++) scanf("%d",&s[i]);
        for(int i=2;i<=n;i++) s[i]+=s[i-1];
        int l=0,r=-1;
        f[0]=0;//千万别忘
        for(int i=1;i<=n;i++)
        {
            while(l<r&&cal(getx(q[r-1]),gety(q[r-1]),getx(q[r]),gety(q[r]),getx(i),gety(i))) r--;
            q[++r]=i;
            while(l<r&&get(q[l],s[i])>=get(q[l+1],s[i])) l++;
            f[i]=get(q[l],s[i])+m+s[i]*s[i];
        }
        printf("%d\n",f[n]);
    }
    return 0;
}


你可能感兴趣的:(斜率优化,2010多校)