【斜率优化dp】poj 1260 Pearls

小天使我爱你~muaaaaa~跟随小天使的步伐学会了【模板】于是可以在20分钟内A简单的dp题啦~虽然dp方程还是不太会推,但是只要会优化就完成近阶段任务了嘛。
这个题的dp方程:
dp[i]=(a[i]+10)*p[i]+dp[i-1]

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node {
    int a, p;
}pearl[105];
int t, n, dp[105], queue[105], sum[105], head, tail;
int getx( int k, int j ) {
    return dp[k] - dp[j];
}
int gety( int k, int j ) {
    return sum[k] - sum[j];
}
void updata() {
    head = 0, tail = 1;
    memset(queue, 0, sizeof(queue));
    memset(dp, 0, sizeof(dp));
    memset(sum, 0, sizeof(sum));
}
int main() {
    scanf("%d", &t);
    while(t--) {
        updata();
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) {
            scanf("%d%d", &pearl[i].a, &pearl[i].p);
            sum[i] = sum[i - 1] + pearl[i].a;
        }
        for(int i = 1; i <= n; i++) {
            while(head + 1 < tail && getx(queue[head + 1], queue[head]) <= gety(queue[head + 1], queue[head]) * pearl[i].p)
                head++;
            dp[i] = dp[queue[head]] +( sum[i] - sum[queue[head]] + 10 ) * pearl[i].p;
            while(head + 1 < tail && getx(i, queue[tail - 1]) * gety(queue[tail - 1], queue[tail - 2]) <= getx(queue[tail - 1], queue[tail - 2]) * gety(i, queue[tail - 1]))
                tail--;
            queue[tail++] = i;
        }
        printf("%d\n", dp[n]);
        }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值