HDOJ 1300 Pearls 斜率优化dp
原题连接: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; }