Codeforces 480D Parcels(dp)
来源:互联网 发布:淘宝网排行榜从哪里进 编辑:程序博客网 时间:2024/04/29 17:05
题目链接:Codeforces 480D Parcels
题目大意:就是有一个用来堆放货物的板,承重力为S。现在有N件货物,每件货物有到达的时间,运走的时间,以及
重量,承重,存放盈利。如果这件货物能再运达时间存放,并在指定时间取走的话,就能获得相应的盈利值。货物都是
逐个往上叠的,每个箱子上面的总重量不能大于箱子的承重。总的质量不能大于板的承重,货物上面还有货物的话是不
能被取走的。现在求最大的盈利值。
解题思路:dp好题,没想出来,看懂别人代码A掉的。
首先将货物按照运走时间早的,运进时间晚的排序,贪心的思想,越早处理掉越早把钱赚了。然后dp[i][j]表示说装入第i
个货物,并且当前重量为j的最大收益。
类似与逆思想的方式,从先移动出去的货物开始考虑,然后向后转移,因为会有说in3,in1,out1,in2,out2,out3这
样的情况,即货物1和货物2的重量是不需要叠加的,两个钱都赚的话也不会影响到货物3,所以f[i]数组用来维护说在i时
刻前已经移走的货物净赚最大值。在考虑第i个货物的时候,需要判断前面i-1个货物,只有在in_i ≤ in_j && out_j ≤ out_i
的时候,才能转移,转移的过程中一并维护f数组。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 500;struct Parcel { int in, out, w, s, v; void read() { scanf("%d%d%d%d%d", &in, &out, &w, &s, &v); }}p[maxn];inline bool cmp (const Parcel& a, const Parcel& b) { return a.out < b.out || (a.out == b.out && a.in > b.in);}int N, S, dp[maxn + 5][maxn * 2 + 5], f[maxn * 2 + 5];int main () { scanf("%d%d", &N, &S); p[0] = (Parcel){0, 2 * maxn, 0, S, 0}; for (int i = 1; i <= N; i++) p[i].read(); sort(p, p + N + 1, cmp); for (int i = 0; i <= N; i++) { for (int w = p[i].w; w <= S; w++) { int o = p[i].in; int wi = min(p[i].s, w - p[i].w); f[o] = 0; for (int j = 0; j < i; j++) if (p[j].in >= p[i].in) { while (o < p[j].out) { o++; f[o] = f[o-1]; } f[o] = max(f[o], f[p[j].in] + dp[j][wi]); } dp[i][w] = f[o] + p[i].v; } } printf("%d\n", dp[N][S]); return 0;}
3 1
- Codeforces 480D Parcels(dp)
- CF-480D-Parcels
- 【DP】CodeForces 148D
- Codeforces 221D(dp)
- Codeforces 572D,DP
- codeforces 9D DP
- codeforces 519D dp
- Codeforces 358D【DP】
- codeforces 474D DP
- Codeforces 830D [DP]
- Codeforces 161D 树形DP
- 【数位DP】 【CodeForces 55D】
- codeforces 55D 数位DP
- Codeforces 61D--树形dp
- Codeforces 413D 2048(dp)
- codeforces 401D (数位DP)
- codeforces 148D 概率DP
- codeforces 474D Flowers dp
- poj2082 Terrible Sets 单调栈
- UVA - 270 Lining Up 数据优化
- Nordic iBeacon设备在ios8上无法正常工作的问题
- MYSQL的一知半解
- 【Java】解决Acces restriction: The type X is not accessible due to restriction on required library 的解决方法
- Codeforces 480D Parcels(dp)
- 2014上海全国邀请赛1010(hdu 5099)
- 【索引】Codeforces Round #274
- Visual Assist X 破解版使用步骤
- 终端中的乐趣:6个有趣的Linux命令行工具
- JQuery学习一
- 遇到多个构造器参数时要考虑用构建器
- 设计模式之策略模式
- 刷单、放单的安全平台!QT语音平台房间1612接待部枫月专业为你打造淘宝爆款