7-Drop Pie (array dp)

Source: Internet
Author: User

/* Free Pies
Time limit: Ms | Memory Limit: 65535 KB
Difficulty: 3

Describe
Said the sky will not drop pies, but one day Gameboy is walking home on the path, suddenly fell into the sky a lot of pie. Gameboy's character was so good that the pie was not lost elsewhere, and fell within 10 metres of his side. If the pie fell on the ground, of course, it could not eat, so Gameboy immediately remove the backpack to pick up. But because the trails could not stand on either side, he could only pick it up on the path. As Gameboy usually stay in the room to play games, although in the game is a skill Agile master, but in the reality of the motor is particularly dull, every second species only in the move not more than a meter in the range to catch falling pies. Now mark the path with the coordinates:

To make the problem easier, let's say that over the next period of time, the pie drops in 0-10 of these 11 positions. At the beginning Gameboy stood at 5, so in the first second he could only receive a 4,5,6 in one of these three positions. Q. How many pies can Gameboy receive? (assuming his backpack can hold an infinite number of pies)

Input
There are multiple sets of input data. The first behavior of each group of data is a positive integer n (0<n<100000), which indicates that there are n pies falling on the path. In the row of n rows, each row has two integers x,t (0<t<100000), indicating that there is a pie drop at x point in T-second. The same second may drop multiple pies at the same point. N=0 when the input ends.
Output
Each set of input data corresponds to one row of output. Output an integer m, indicating that Gameboy may receive a maximum of M pies.
Tip: The amount of input data in the subject is relatively large, it is recommended to read in scanf, with CIN may time out.
Sample input

6
5 1
4 1
6 1
9 |
9 |
8 3
0

Sample output

4
*/
The idea is actually very clear, is the recursive optimal, draw a coordinate system, to x,t respectively for the horizontal ordinate, look at the relevant points between the path, you will find that is actually the evolution of the digital triangle
It is worth noting that the relationship fun (t, x) = Max{fun (t+1,x-1) + Fun (t+1, x) + Fun (t+1,x+1)} + a[t][x] and starting point, while paying attention to the processing of the boundary

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace Std;
int dp[100005][12];
int a[100005][12];
int m;

int fun (int t, int x) {
if (dp[t][x]! = 0)
return dp[t][x];
if (t = = m)
return dp[t][x] = a[t][x];
if (x = = 0)//See the topic and start thinking it's from the beginning
return dp[t][x] = max (Fun (t + 1, x), fun (t + 1, x + 1) + a[t][x]; To consider boundary issues
if (x = = 10)
return dp[t][x] = max (Fun (t + 1, x-1), fun (t + 1, x)) + a[t][x];
int max1 = max (Fun (t + 1, x-1), fun (t + 1, x));
return dp[t][x] = max (Max1,fun (t + 1, x + 1) + a[t][x];
}

int main () {
int n;
while (~SCANF ("%d", &n)) {
if (n = = 0)
return 1;
memset (A, 0, sizeof (a));
memset (DP, 0, sizeof (DP));
m = 0;
int x, t;
for (int i = 0; i < n; i++) {
scanf ("%d%d", &x, &t);
a[t][x]++;
m = (M < T? t:m); Record the maximum moment
}
printf ("%d\n", Fun (0, 5));
}
return 0;
}

7-Drop Pie (array dp)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.