POJ 1036 Gangster (动态规划)
2011-10-04 22:45
239 查看
题中,土匪分别有进入时间t,荣誉值p,状态值s,只有当t 时刻,餐厅们的状态和土匪的状态值s一样的时候才能够得到荣誉值p,要求最大的荣誉值获得。
这道题先看便觉得是动态规划的题目,比赛的时候建了一个T和S的动态数组dp,结果出现WA。比赛完之后发现,做全体T和S的dp是非常耗时间和空间的,所以就改一个算法。先对土匪进入时间排序,之后,对每一个土匪进行判断,通过时间看是否可以经过上一个土匪的时间开始,或者本身0开始,进行状态转换,从而得到该荣誉值,如果可以,就存下来,再用他去更新下一个值,知道所有状态更新完全,输出最大值,即可。
这道题先看便觉得是动态规划的题目,比赛的时候建了一个T和S的动态数组dp,结果出现WA。比赛完之后发现,做全体T和S的dp是非常耗时间和空间的,所以就改一个算法。先对土匪进入时间排序,之后,对每一个土匪进行判断,通过时间看是否可以经过上一个土匪的时间开始,或者本身0开始,进行状态转换,从而得到该荣誉值,如果可以,就存下来,再用他去更新下一个值,知道所有状态更新完全,输出最大值,即可。
#include<iostream> #include<algorithm> #include<cmath> using namespace std; const int MAXN =110; struct Gangster { int t, p,s; bool operator < (const Gangster &g) const { return t < g.t; } }g[MAXN]; int main() { int i, j, n, k, t; cin >> n >> k >> t; for(i = 0; i < n; i++) scanf("%d", &g[i].t); for(i = 0; i < n; i++) scanf("%d", &g[i].p); for(i = 0; i < n; i++) scanf("%d", &g[i].s); sort(g, g + n); int best[MAXN], ans, dt, ds; ans = 0; for(i = 0; i < n; i++) { best[i] = -1; if(g[i].t < g[i].s) continue; best[i] = 0; for(j = 0; j < i; j++) if((best[j] > best[i])&&(g[i].t-g[j].t) >= abs(g[i].s - g[j].s)) best[i] = best[j]; best[i] += g[i].p; if(best[i] > ans) ans = best[i]; } printf("%d\n", ans); return(0); }
相关文章推荐
- poj 1036 Gangster -- 区间型DP解法
- poj 1036 Gangster -- 最长上升子序列解法
- POJ1036——动态规划
- POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心)-动态规划做法
- POJ - 3265/USACO - Jan07 Gold Problem Solving 动态规划(DP) | 对USACO官方题解0ms程序的解释
- poj1036 dp
- POJ-2029 Get Many Persimmon Trees(动态规划)
- Apple Catching POJ - 2385 --简单动态规划
- poj 2229 动态规划
- POJ 1036
- poj 1088 滑雪 动态规划
- Poj1159 Palindrome(动态规划DP求最大公共子序列LCS)
- poj 3666 Making the Grade 动态规划+滚动数组
- POJ3624 - Charm Bracelet - 动态规划之01背包
- [POJ 1080]Human Gene Functions(动态规划)
- poj&nbsp;3661&nbsp;Running&nbsp;dp(动态规划)
- [POJ](1661)Help Jimmy ---- 动态规划
- POJ 动态规划题目列表
- [poj]动态规划2479
- POJ2411 Mondriaan's Dream (轮廓线动态规划典型例题 以及用状态压缩逐行深搜推的方法)