HDU 1176 掉馅饼

题目: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
思路:例子中移动路径为6 7 8。得到4个馅饼。 刚开始想着用dfs,但是看了下数据,会超时。所以直接用dp,枚举所有情况找到最优。DP【T】[X]表示在X点T时掉下的馅饼数量,然后往上推,dp[T][X]=max(dp[T][X+1],DP[T][X],DP[T][X+1]).最后找到DP[0][5].回到起点。

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值