【HDU1176】Free pie (type count tower problem)

Free pie

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Problem Description

It is said that there will be no pies in the sky, but one day the gameboy was walking on the path home, and suddenly a lot of pies fell from the sky. Said gameboy's character is really good, this pie did not fall anywhere else, it fell within 10 meters of his side. If the pie falls on the ground, of course it can't be eaten, so the gameboy immediately removed his backpack to pick it up. But because there is no one on both sides of the trail, he can only pick up on the trail. Because gameboy usually stays in the room to play games, although he is a very agile player in the game, his motor nerves are particularly slow in reality, and he can only catch falling pies within a range of no more than one meter per second. Now give the coordinates of this trail like the icon:
Write picture description here

In order to simplify the problem, suppose that in the next period of time, the pie will fall in the 11 positions of 0-10. At the beginning, the gameboy stood at position 5, so in the first second, he could only receive the pie in one of the three positions 4, 5, and 6. How many pies can gameboy receive at most? (Assuming that his backpack can hold an infinite number of pies)

Input

There are multiple groups of input data. The first line of each group of data is a positive integer n(0

Output

Each set of input data corresponds to one line of output. Output an integer m, indicating that gameboy may receive at most m pies.

Tip : The amount of input data for this question is relatively large. It is recommended to use scanf to read in. Using cin may time out.

Sample Input

6
5 1
4 1
6 1
7 2
7 2
8 3
0

Sample Output

4

Problem analysis:
Write picture description here

As shown above, there are dp[][] with N rows and M columns

N stands for seconds, M stands for this coordinate

When one second comes, we can choose to start walking left, right, or down at the point (1,5). So starting from the second line dp[i][j]=max(dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1])+num[i][j], the largest of the three plus the number of pie in this place is the maximum number of pie that can be eaten at this point.

So we pushed to the last line to find the largest number is the largest number of pie we can eat.

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"

using namespace std;

const int maxn = 1e5+5;

int dp[maxn][12];

int num[maxn][12];

int m;

int a,b,time;

int LL;

int main(){
    while( ~scanf("%d",&m),m ){
        LL = 0;
        memset(num,0,sizeof(num));
        memset(dp,0,sizeof(dp));
        while(m--){
            scanf("%d%d",&b,&a);
            num[a][b]++;
            if(a > LL){
                LL=a;
            }
        }

        dp[1][5]=num[1][5];
        dp[1][4]=num[1][4];
        dp[1][6]=num[1][6];
        for( int i=2 ; i<=LL ;i++ ){
            for( int j=0 ; j<=10 ; j++ ){
                dp[i][j] = max(dp[i-1][j-1],(max(dp[i-1][j],dp[i-1][j+1]))) + num[i][j];
            }
        }
        int ans = 0;
        for( int j=0 ; j<=10 ; j++ ){
            if(dp[LL][j]>=ans){
                ans = dp[LL][j];
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/thesprit/article/details/52026889