hdu4258 斜率优化dp

参考下面文章
http://blog.csdn.net/balloons2012/article/details/7912296
感觉最重要的是  和当前位置有关的斜率,随下表递增单调。。这样凸包才可以求得最优解
</pre><pre name="code" class="cpp">#include <iostream>
#include <cmath>
#include <cstdio>
#define N 1000010
#define LL long long
using namespace std;

struct point{
    LL x, y;
    point(LL a=0, LL b = 0): x(a), y(b) {
    }
    void set(LL a, LL b) {
        x = a;
        y = b;
    }
};

point que[N];
LL a[N];
LL dp[N];

bool mul(const point &a, const point &b, const point &c) {
    return (b.x-a.x) * (c.y-a.y) <= (b.y-a.y)*(c.x-a.x); 
}
int main(int argc, char* argv[]) {
    LL n, c;
    while(scanf("%lld%lld", &n, &c) && (n && c)) {
        for(LL i=0; i<n; ++i) {
            scanf("%lld", &a[i]);
        }
        int head = 0, tail = 0;
        que[tail++].set(a[0], a[0]*a[0]);
        dp[0] = c;
        for(LL i=1; i<n; ++i) {
            point pp(a[i], dp[i-1]+a[i]*a[i]);
            while(head+1 < tail && mul(que[tail-2], que[tail-1], pp)) --tail;
            que[tail++] = pp;
            while(head+1 < tail && que[head].y-2*a[i]*que[head].x >= que[head+1].y-2*a[i]*que[head+1].x) ++head;
            dp[i] = que[head].y - 2*a[i]*que[head].x + a[i]*a[i] + c;
        }
        printf("%lld\n", dp[n-1]);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值