Hdu1176 DP data Tower

Source: Internet
Author: User

After reading the code of Daniel, I suddenly realized that I had to write it myself. Wa had a afternoon!

There are two pitfalls here, so I have been looking for one afternoon!

AC code:

# Include <iostream>
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <iostream>
# Include <string. h>
# Include <algorithm>
Const int maxn = 100000 + 5;
Int DP [maxn] [12];
Using namespace STD;
Int Maxy (int A, int B, int C)
{
Int T;
If (A> B) {T = B; B = A; A = T ;}
If (B> C) {T = C; C = B; B = T ;}
Return C;
}
Int main ()
{
Int n, x, t, t;
While (scanf ("% d", & N )! = EOF & N)
{
T = 0;
Memset (DP, 0, sizeof (DP ));
While (n --)
{
Scanf ("% d", & X, & T );
DP [T] [x] ++;
T = max (t, t );
}
For (INT I = T; I> 1; I --)
For (Int J = 0; j <11; j ++)
{
If (j = 0) 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] + = Maxy (DP [I] [J], DP [I] [J + 1], DP [I] [J-1]);
}
Printf ("% d \ n", Maxy (DP [1] [4], DP [1] [5], DP [1] [6]);
}
Return 0;
}

When defining a large array, do not define the array in the function. Because the array in the function is stored in the stack, the space is not large enough, to define a global array, the global array is stored in the bucket. I looked at Daniel's code and my code. It was really anxious! Fortunately, it was found out;

The second pitfall is that after I store all the data in a two-dimensional array, it is best to use else if and then use else instead of two if. Otherwise, the statement without the if statement with or without else will be executed twice.

For example

If (j = 0) 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] + = Maxy (DP [I] [J], DP [I] [J + 1], DP [I] [J-1]);

If (j = 0) DP [I-1] [J] + = max (DP [I] [J], DP [I] [J + 1]);
If (j = 10) DP [I-1] [J] + = max (DP [I] [J], DP [I] [J-1]);
DP [I-1] [J] + = Maxy (DP [I] [J], DP [I] [J + 1], DP [I] [J-1]);

Let's talk about this. The idea is to use a two-dimensional array to represent and record the dimensions of no time and location, in the same way as the data Tower, add up and find the largest sum of DP [1] [5], that is, from 5.

Hdu1176 DP data Tower

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.