您的位置:首页 > 其它

POJ 1036 Gangster (动态规划)

2011-10-04 22:45 239 查看
题中,土匪分别有进入时间t,荣誉值p,状态值s,只有当t 时刻,餐厅们的状态和土匪的状态值s一样的时候才能够得到荣誉值p,要求最大的荣誉值获得。

这道题先看便觉得是动态规划的题目,比赛的时候建了一个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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: