A Simple Dynamic Programming Problem
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 78 accepted submission (s): 25
Problem descriptiondragon is watching nba. He loves James and Miami Heat.
Here's an introduction of Basketball Game: http://en.wikipedia.org/wiki/Basketball. However the game in dragon's version is much easier:
"There's two teams fight for the winner. the only way to gain scores is to throw thebasketball into the basket. each time after throwing into the basket, the score gained by the team is 1, 2 or 3. however due to the uncertain factors in the game, it's hard to predict which team will get the next goal ".
Dragon is a crazy fan of miami heat so that after each throw, he will write down the difference between two team's score regardless of which team keeping ahead. for example, if heat's score is 15 and the opposite team's score is 20, Dragon will write down 5. on the contrary, if heat has 20 points and the opposite team has 15 points, Dragon will still write down 5.
Several days after the game, dragon finds out the paper with his record, but he forgets the result of the game. it's also fun to look though the differences without knowing who lead the game, for there are so cute uncertain! Dragon loves uncertain, and he wants to know how many
ResultsCocould the game has gone?
Inputthe first line of input contains only one integer t, the number of test cases. Following t blocks, each block describe one test case.
For each test case, the first line contains only one integer n (n <= 100000), which means the number of records on the paper. then there comes a line with N integers (A1, A2, A3 ,..., an ). AI means the number of I-th record.
Outputeach output shocould occupy one line. Each line shocould start with "case # I:", with I implying the case number. Then for each case just puts an integer, implying the number
ResultCocould the game has gone.
Sample Input
222 341 3 5 7
Sample output
Case #1: 2Case #2: 2
Source2014 multi-university training contest 10
Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4972
The basketball team scored two-point and three-point goals. Now, the difference between the two teams is recorded. (at that time, my teammates wa many times, because only one of them is recorded here) the score difference is leading-backward. For example, Team A scored 10 points, Team B scored 5 points, and team A scored 5 points. When Team A scored 15 points and Team B scored 20 points, the score difference remained 5, the final requirement is the number of possible values in the final match result. The data is not guaranteed to be valid and must be determined by yourself.
Question Analysis: Question traps .. A simple dynamic planning... In fact, it is just a simple simulation. The key is the formula deduction in two cases. Because we know the final score difference, the two cases mean that the final score difference is 0 and not 0, we only need to consider the two sides of the score and the number of cases, because each time can only get 1, 2 or 3 points, obviously there are multiple possibilities only when the score difference is 2-> 1 or 1-> 2, for example, if the first score is 1 after 2, A may lead to B2, and then B gets 1, then a leads to B1, or B gets 3, and B leads to A1, 1-> 2. Similarly, in other cases, the score can only have one increment, without changing the number of the current types. We can use count to record the number of adjacent records as 1, 2, or 2, the score is equal to or greater than count,
Then let's look at the last split. If it is 0, it indicates that a and B are flattened. The number of classes is Count + 1. If the last split is not 0, obviously, a wins B or B wins a, and there are 2 (count + 1) types. We also need to discuss illegal cases. For details, see program comments.
The last two simple examples are as follows:
2 1 0 A = 2, B = 1-> A = 2, B = 2
Assume that in the first two cases, A = 2, B = 0-> A = 2, B = 3-> A = 3, B = 3.
A = 0, B = 2-> A = 1, B = 2-> A = 2, B = 2
A = 3, B = 2-> A = 3, B = 3
2 1 1
Assume that a = 2, B = 0-> A = 2, B = 1-> A = 2, B = 3
A = 2, B = 3-> A = 4, B = 3
Assume that a = 0, B = 2-> A = 1, B = 2-> A = 3, B = 3
A = 3, B = 2-> A = 3, B = 4
# Include <cstdio> # define ll long longint const max = 100000 + 2; int A [Max]; int main () {int N, T, flag; ll ans, count; scanf ("% d", & T); For (int ca = 1; Ca <= T; CA ++) {COUNT = 0; flag = 1; scanf ("% d", & N); For (INT I = 1; I <= N; I ++) scanf ("% d ", & A [I]); If (n = 0) {// n = 0, only one case is equal to printf ("case # % d: 1 \ n ", CA); continue;} if (a [1] = 0) // the time difference of the first record cannot be 0 {printf ("case # % d: 0 \ n ", CA); continue;} for (in T I = 1; I <= n-1; I ++) {// invalid situation: // There is a score difference less than 0, and each two scores is greater than 3, every two distributions are equal and the values are not 1 if (a [I] <0 | A [I + 1]-A [I]> 3 | A [I]- A [I + 1]> 3 | (A [I] = A [I + 1] & A [I]! = 1) {flag = 3; break;} // record the case of 1-> 2 if (a [I] = 1) {flag = 1; if (A [I + 1] = 2) Count ++;} // record 2-> 1 case if (a [I] = 2) {flag = 1; if (a [I + 1] = 1) Count ++ ;}// if the last score difference is 0, this is the second case, if (A [n] = 0) Flag = 2; // use the formula to calculate the result if (flag = 1) printf ("case # % d: % i64d \ n ", CA, 2 * (count + 1); else if (flag = 2) printf (" case # % d: % i64d \ n ", CA, Count + 1); else if (flag = 3) printf ("case # % d: 0 \ n", CA );}}