[HDU 1176] 免费馅饼 - 动态规划

Question

都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米范围内。馅饼如果掉在了地上当然就不能吃了,所以gameboy马上卸下身上的背包去接。但由于小径两侧都不能站人,所以他只能在小径上接。由于gameboy平时老呆在房间里玩游戏,虽然在游戏中是个身手敏捷的高手,但在现实中运动神经特别迟钝,每秒种只有在移动不超过一米的范围内接住坠落的馅饼。现在给这条小径如图标上坐标:
在这里插入图片描述
Sorry, I am not sure how to remove the water mark. If you know please contact with me:)…
.
为了使问题简化,假设在接下来的一段时间里,馅饼都掉落在0-10这11个位置。开始时gameboy站在5这个位置,因此在第一秒,他只能接到4,5,6这三个位置中其中一个位置上的馅饼。问gameboy最多可能接到多少个馅饼?(假设他的背包可以容纳无穷多个馅饼)
.
Input:
输入数据有多组。每组数据的第一行为以正整数n(0<n<100000),表示有n个馅饼掉在这条小径上。在结下来的n行中,每行有两个整数x,T(0<T<100000),表示在第T秒有一个馅饼掉在x点上。同一秒钟在同一点上可能掉下多个馅饼。n=0时输入结束。
.
Output:
每一组输入数据对应一行输出。输出一个整数m,表示gameboy最多可能接到m个馅饼。
提示:本题的输入数据量比较大,建议用scanf读入,用cin可能会超时。

Sample Input:
6
5 1
4 1
6 1
7 2
7 2
8 3
0

Sample Output:
4

Stragedy

We can use DP to solve the problem. The number of one cake’s he can catch equals one plus the one up left, up, and up right.

Here is the graph of the cakes:
3’s second: 0 0 0 0 0 0 0 0 1 0 0
2’s second: 0 0 0 0 0 0 0 2 0 0 0
1’s second: 0 0 0 0 1 1 1 0 0 0 0
0’s second: 0 0 0 0 0 0 0 0 0 0 0
(0 means no cake at that position)
Each row stand for at each time the position cakes will drop at

状态转移方程: dp[i][j]+=max(max(dp[i+1][j],dp[i+1][j+1]),dp[i+1][j-1])

Code

Now here is the code y’all been waiting for:)

#include <bits/stdc++.h>
using namespace std;
int dp[100002][12]={0},cakes,i,place,t,start=0;
int main(){
    ios::sync_with_stdio(0); // This increase the speed of input
    while (cin>>cakes){
        if (cakes==0) break;
        memset(dp, 0, sizeof(dp));
        for (i=0;i<cakes;i++){
            cin>>place>>t;
            dp[t][place]++;
            start=max(start,t);
        }

        // The grid
        /*for (i=start;i>=0;i--) {
            for(int j=0;j<=10;j++) cout<<dp[i][j]<<" ";
            cout<<endl;
        }*/

        for (i=start-1;i>=0;i--) { // (start-1) No need to check the top row
            for (int j=0;j<=10;j++) {
                dp[i][j]+=max(max(dp[i+1][j-1],dp[i+1][j]),dp[i+1][j+1]);
            }
        }
        cout<<dp[0][5];

    }
    return 0;
}

😃

  • 35
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值