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