【斜率优化dp】poj 1260 Pearls
2017-05-19 21:31
399 查看
小天使我爱你~muaaaaa~跟随小天使的步伐学会了【模板】于是可以在20分钟内A简单的dp题啦~虽然dp方程还是不太会推,但是只要会优化就完成近阶段任务了嘛。
这个题的dp方程:
dp[i]=(a[i]+10)*p[i]+dp[i-1]
这个题的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 ); } return 0; }
相关文章推荐
- poj 1260 Pearls 斜率优化dp
- poj 1260 Pearls 斜率优化dp
- [poj1260] Pearls DP斜率优化
- poj 1260 Pearls 斜率优化dp
- 【POJ1260】【斜率优化DP】Pearls题解
- Poj1180 Batch Scheduling --- DP的斜率优化
- poj 1180 Batch Scheduling 斜率优化dp
- poj 1180 斜率优化DP
- poj 3709 K-Anonymous Sequence 斜率优化dp
- poj 3709 K-Anonymous Sequence 斜率优化dp 分组有大小限制
- Poj1180 Batch Scheduling --- DP的斜率优化
- poj 1180 Batch Scheduling ( 斜率优化DP )
- poj 1180 斜率优化dp
- poj 3709 K-Anonymous Sequence(斜率优化DP)
- POJ - 1260 Pearls 区间dp
- POJ 1260 Pearls (基础DP)
- POJ 1260 Pearls(DP)
- 斜率优化DP(POJ1180 && POJ3709)
- POJ 1180 Batch Scheduling(斜率优化DP)
- poj 1180 Batch Scheduling 斜率优化dp