C - Free pie HDU - 1176 (Reverse DP)

He said the sky will not fall, but one day gameboy walking down the trail back home, suddenly fall from the sky a lot a lot of pie. Say gameboy character is wonderful, this pie is not out elsewhere, they fall within a range of 10 meters beside him. If the pie out of the ground, of course not eat, so gameboy immediately remove the body bag to pick up. But because both sides of the trail can not stand, so he can only take on the trails. As the old gameboy usually stay in the room to play games, though a nimble master in the game, but in reality the movement is particularly slow nerve, and only in the mobile per second does not exceed one meter range to catch falling pie. Now the trail as the icon coordinates:

In order to simplify the problem, assume that in the next period of time, which pies are falling 0-10 11 positions. Gameboy standing position at the beginning of this 5, so the first second, he received only three positions 4,5,6 in a position where the pie. Q gameboy can take up to a number of pie? (Assuming his backpack can hold an infinite number of pie)
the Input
input data a plurality of sets. The first row of each set of data to a positive integer n (0 <n <100000) , there are n represents pie out on this path. N junction down the row, each row has two integers x, T (0 <T < 100000), shows a pie of T seconds off at the point x. The same second at the same point may fall more pie. n = 0, the input end.
Output
of each set of input data corresponding to one line of output. Output an integer m, m represents gameboy can take up to a pie.
Tip: Enter data to this question than the larger, recommended read with scanf, with cin may time out.

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

Ideas:
Reverse DP, because the state is only a start and end state can be many.
Order DP [i] [j] representative of the time to the maximum number of squares pie i j, the result is dp [0] [5].

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int dp[100005][25];

int main()
{
    int n;
    while(~scanf("%d",&n) && n)
    {
        int m = 0;
        memset(dp,0,sizeof(dp));
        for(int i = 1;i <= n;i++)
        {
            int x,t;scanf("%d%d",&x,&t);
            m = max(m,t);
            dp[t][x]++;
        }
        
        for(int i = m - 1;i >= 0;i--)
        {
            for(int j = 0;j <= 10;j++)
            {
                dp[i][j] += max(dp[i + 1][j],max(dp[i + 1][j - 1],dp[i + 1][j + 1]));
            }
        }
        
        printf("%d\n",dp[0][5]);
    }
    return 0;
}
Published 676 original articles · won praise 18 · views 30000 +

Guess you like

Origin blog.csdn.net/tomjobs/article/details/104230307