题目:http://acm.hdu.edu.cn/showproblem.php?pid=1176
在0-10的坐标上有n个馅饼掉落,求能接到的最多馅饼数。初始位置在5,每秒移动一格。
Sample Input
6 5 1 4 1 6 1 7 2 7 2 8 3 0
Sample Output
4
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 100000 + 5;
int main()
{
int dp[maxn][12];
int n, i, j, maxday, x, t,temp;
while (~scanf("%d", &n) && n != 0)
{
memset(dp, 0, sizeof(dp));
maxday = 0;
for (i = 1; i <= n; i++)
{
scanf("%d%d", &x, &t);
dp[t][x]++;//第t天在x有落下
maxday = max(maxday, t);//找到最晚落下来的天数
}
for (i = maxday - 1; i >= 0; i--)//从小往上更新,
{
dp[i][0] += max(dp[i + 1][0], dp[i + 1][1]);//边界情况
for (j = 1; j <= 10; j++)
{
temp = 0;
temp = max(dp[i + 1][j], dp[i + 1][j + 1]);
temp = max(temp, dp[i + 1][j - 1]);
dp[i][j] += temp;
}
}
cout << dp[0][5] << endl;
}
return 0;
}