http://acm.guet.edu.cn/problemset/problem/1116
数塔模型
自底向上求解
dp【i】【j】 = 第i分钟,位置j的最大馅饼数。
dp【i】【j】 = Max{ dp【i+1】【j-1】, dp【i+1】【j】, dp【i+1】【j+1】}
注意边界。答案为dp【0】【5】(起点)。
由于数组下标搞错,WA了很多次!
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX_N = 100001, MAX_T = 100001;
int N, T;
int dp[MAX_N][12];
int main()
{
//freopen("in.txt", "r", stdin);
while(scanf("%d", &N)!=EOF && N)
{
memset(dp, 0, sizeof(dp));
int maxT = -1;
for(int i = 0; i < N; i++)
{
int t, pos;
scanf("%d%d", &pos, &t); //时间;位置
dp[t][pos]++;
maxT = max(maxT, t);
}
for(int i = maxT-1; i >= 0; i--)
for(int j = 0; j < 11; j++)
{
if(j == 0) dp[i][j] += max(dp[i+1][j], dp[i+1][j+1]);
else if(j == 10) dp[i][j] += max(dp[i+1][j-1], dp[i+1][j]);
else dp[i][j] += max(max(dp[i+1][j-1], dp[i+1][j]), dp[i+1][j+1]);
}
printf("%d\n", dp[0][5]);
}
return 0;
}