HDU 1176 Free Pie (dp)

free pie

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 59947    Accepted Submission(s): 21048


Problem Description
It is said that no pies will fall from the sky, but one day Gameboy was walking on the path home when suddenly a lot of pies fell from the sky. Speaking of gameboy's character is really good, this pie doesn't fall anywhere else, it just falls within 10 meters of him. Of course, the pie can't be eaten if it falls on the ground, so gameboy immediately removes his backpack to pick it up. But since no one could stand on either side of the trail, he had to pick it up on the trail. Because gameboy usually stays in the room to play games, although he is a master of agility in the game, in reality, his motor nerves are particularly slow, and he can only catch the falling pie within a range of no more than one meter per second. Now give this trail the coordinates on the icon:

To simplify the problem, assume that the pie falls in the 11 positions 0-10 for the next period of time. Gameboy starts at 5, so in the first second, he can only receive a pie on one of the three positions 4, 5, and 6. Ask the gameboy how many pies at most? (Assuming his backpack can hold an infinite number of pies)
 

Input
There are multiple sets of input data. The first row of each set of data is a positive integer n (0<n<100000), indicating that there are n pies falling on this trail. In the resulting n lines, each line has two integers x, T (0<T<100000), which means that a pie falls on point x at the T second. Multiple pies may fall at the same point in the same second. The input ends when n=0.
 

Output
Each set of input data corresponds to a line of output. Output an integer m, indicating that gameboy may receive at most m pies.
Tip: The amount of input data in 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
 
You can use the number tower idea to do this question, bottom-up, or ordinary dp (easy to understand)

Similar to the number tower:

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int dp[100005][13];//dp[i][j] The pie in the ith second appears at the jth point
intmain()
{
    int n,a,b;
    while(~scanf("%d",&n)&&n)
    {
        memset(dp,0,sizeof(dp));
        int time=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a,&b);
            dp[b][a+1]++;
            if(time<b)//Total seconds
                time=b;
        }
        int maxn=0;
        for(int i=time-1;i>=0;i--)
            for(int j=1;j<=11;j++)
            {
                dp[i][j]=max(dp[i+1][j+1],max(dp[i+1][j],dp[i+1][j-1]))+dp[i][j];
            }
        printf("%d\n",dp[0][6]);//Output at the starting point
    }
    return 0;
}

Normal dp:

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int dp[100005][13];
intmain()
{
	int n,a,b;
	while(~scanf("%d",&n)&&n)
	{
		memset(dp,0,sizeof(dp));
		int time=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&a,&b);
			dp[b][a]++;
			if(time<b)
				time=b;
		}
		for(int i=0;i<=10;i++)
		{//Initialization starting point
			if(i==4||i==6||i==5)
			continue;
			dp[1][i]=0;
		}
		int maxn=0;
		for(int i=1;i<=time;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]))+dp[i][j];
			}
			for(int i=0;i<=10;i++)
			{// Traverse the last time maximum value
				if(maxn<dp[time][i])
					maxn=dp[time][i];
			}
		printf("%d\n",maxn);
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325939316&siteId=291194637