您的位置:首页 > 其它

动态规划练习

2016-10-26 18:53 239 查看
hdu 1176题目链接 类似求解背包问题的过程,滚动一遍就ok,或者记忆化搜索也可以

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxTime = 100005;
int num[maxTime][11],dp[maxTime][11];

int main()
{
int n;
while(scanf("%d" ,&n) && n)
{
memset(num,0,sizeof(num));
memset(dp,-1,sizeof(dp));
int fina = -1;
for(int k = 1; k <= n; k++)
{
int x,t;
scanf("%d%d" ,&x,&t);
num[t][x]++;
fina = std::max(fina,t);
}
dp[1][4] = num[1][4]; dp[1][5] = num[1][5]; dp[1][6] = num[1][6];
for(int time = 2; time <= fina; time++)
{
for(int pos = 0; pos <= 10; pos++)
{
int maxt = -1;
for(int k = -1; k <= 1; k++)
{
int p = pos + k;
if(p < 0 || p > 10) continue;
if(dp[time-1][p] < 0) continue;
maxt = max(maxt,dp[time-1][p]);
}
if(maxt >= 0)
{
dp[time][pos] = maxt + num[time][pos];
}
}
}
int ans = 0;
for(int pos = 0; pos <= 10; pos++) ans = max(ans,dp[fina][pos]);
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: