斜率优化入门

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;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值