码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 1300 Pearls 斜率优化dp

时间:2015-10-08 13:10:02      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

原题连接:http://acm.hdu.edu.cn/showproblem.php?pid=1300

题意:

题目太长了。。自己看吧

题解:

看懂题目,就会发现这是个傻逼dp题,斜率优化一下就好

代码:

#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#define MAX_N 500
using namespace std;

typedef long long ll;

ll N;
ll sum[MAX_N];

ll dp[MAX_N];
ll a[MAX_N];
ll p[MAX_N];

double Y(int t){
    return dp[t];
}

double X(int t){
    return sum[t];
}

double Slope(int u,int v) {
    return (Y(u) - Y(v)) / (X(u) - X(v));
}

int que[MAX_N];
int T;
int main() {
    cin.sync_with_stdio(false);
    cin>>T;
    while (T--) {
        cin>>N;
        memset(dp,0,sizeof(dp));
        memset(que,0,sizeof(que));
        memset(sum,0,sizeof(sum));
        for (int i = 1; i <= N; i++) {
            cin >> a[i] >> p[i];
            sum[i] = sum[i - 1] + a[i];
        }
        int front = 0, rear = 0;
        que[rear++] = 0;

        for (int i = 1; i <= N; i++) {
            while (rear - front > 1 && Slope(que[front], que[front + 1]) <= p[i])front++;
            int j = que[front];
            dp[i] = dp[j] + (sum[i] - sum[j] + 10) * p[i];
            while (rear - front > 1 && Slope(que[rear - 1], que[rear - 2]) >= Slope(que[rear - 1], i))rear--;
            que[rear++] = i;
        }
        cout << dp[N] << endl;
    }
    return 0;
}

 

HDOJ 1300 Pearls 斜率优化dp

标签:

原文地址:http://www.cnblogs.com/HarryGuo2012/p/4860587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!