Hdu1176 free pie

Source: Internet
Author: User

Simple dp

Requirement: calculate the maximum value from dp [0] [5] to dp [t] []

Similar to the data Tower, only 1 ~ The number 9 has three directions: 0 and 10. There are only two options.

We can regard all the time periods and pies as a matrix. Time is the number of rows, and the number of columns is the number triangle. We can find a path from the bottom layer, make the sum on the path largest.

 


Dp [I] [j] indicates the maximum value of the j position at the I moment.

Do not understand why for t-> 0 for 1-> 10 and then get dp [0] [5]

First, we need to push forward from the last second, just as the number tower starts to calculate from the bottom layer.

Then calculate the maximum value that can be obtained for each position. because you do not know whether the next position in these locations is the end point, every position must be calculated from 0-> t.

 

# Include <iostream> # include <cmath> # include <cstdio> # include <vector> # include <cstring> # include <algorithm> using namespace std; int dp [100005] [15], n, t; int max3 (int a, int B, int c) {int m = 0; if (a> m) m = a; if (B> m) m = B; if (c> m) m = c; return m;} int main () {int I, j,, b; while (scanf ("% d", & n) {memset (dp, 0, sizeof dp); t = 0; while (n --) {scanf ("% d", & a, & B); dp [B] [a] ++; t = max (t, B );} for (I = t; I> 0; I --) // push forward from the last second {for (j = 0; j <= 10; j ++) // calculate the maximum value with j as the end point every second {if (j = 0) // j = 0 or 10 only select {dp [I-1] [j] + = max (dp [I] [j], dp [I] [j + 1]);} else if (j = 10) {dp [I-1] [j] + = max (dp [I] [j], dp [I] [J-1]);} else {dp [I-1] [j] + = max3 (dp [I] [j], dp [I] [J-1], dp [I] [j + 1]) ;}} printf ("% d \ n", dp [0] [5]);} return 0 ;}

 

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.