HDU--1176(DP)

Address: http://acm.hdu.edu.cn/showproblem.php?pid=1176

    The meaning of the Chinese title is not much explained.

    Create a two-dimensional dp array, dp [i] [j] means a pie that falls on j at the i-th second. We need to reverse the DP. Why, starting from 0 seconds, x = 5, if we go down the array, we do n’t know where the end point is. The maximum value along this route may not be the next second. The maximum value. But we know the starting point, so we start from the end point and go to dp [0] [5]. There are many unknown situations in this general route, but one thing we can determine:

            i , j

     i+1,j-1       i+1,j       i+1,j+1

    For dp [i] [j], if you want to go to the next step, you must add the larger value of the next step. This thing will not change. So from the last time point max-1 (actually from max can also be passed), go up, each time take the maximum value of the three numbers in the next row of i, j, and then add i, j. Get the equation:

    dp【i】【j】+= max(dp【i+1】【j-1】,dp【i+1】【j】,dp【i+1】【j+1】);

    Finally, output dp [0] [5] as the starting point.

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn=1e5+10;
int dp[maxn][12];
int main()
{
    int n;
    while(scanf("%d",&n))
    {
        if(n==0)    
            break;
        memset(dp,0,sizeof(dp));
        int t,x;
        int maxx=-1;
        for(int i=1 ;i <= n;i++)
        {
            scanf("%d%d",&x,&t);
            dp[t][x]++;
            if(t>maxx)
                maxx=t;
        }
        for(int i=maxx-1;i>=0;i--)    
        {
            for(int j=0;j<=10;j++)
            {
                  int mid = max(dp[i+1][j-1],dp[i+1][j]);
                  dp[i][j]+=max(mid,dp[i+1][j+1]);
            }
        }
        printf("%d\n", dp[0][5]);     
    }
}

 

Guess you like

Origin www.cnblogs.com/liyexin/p/12683102.html