The title of the zoj monthly competition is a very good DP ..
A one-dimensional 2048 game
Merge as long as there are adjacent similarities. After the merge, there will be a reward score, N in total, each of which can be obtained or not obtained.
Ask the final maximum value
Data range: n <= 500, a [I] = {2, 4, 8, 16 };
Analysis:
First, let's clarify the meaning of automatic merge. For example, the original values 8, 4, and 2 will change to 16 if you enter 2.
So we need to record all the preceding numbers .. After calculation, the maximum number of records found is 500, which is 4096 = 2 ^ 12.
Obviously, all records are impossible. How can this problem be solved?
We found that only the descending sequence can be merged forward .. So we only need to record the descending sequence at the end of a State.
The maximum number is only 2 ^ 12, so the number of descending sequences is only 2 ^ 13-1 and can be recorded ..
This is the status transfer problem.
The status of the current number is not changed.
Take the current number to three conditions
1. If there is a smaller number than the current number, then if this number is obtained, the descending sequence will only have this number.
2. The end of the previous part is exactly equal to the current number, so it is merged up until it cannot be merged.
3. If the front end is greater than the current number, insert the current number directly into the status.
The specific implementation depends on the Code. It is interesting to use a bit of bitwise operations.
# Include <iostream> # include <stdio. h> # include <string. h> # include <algorithm> # include <string> # include <ctype. h> using namespace STD; # define maxn short int DP [2] [8200]; int A [505]; int main () {# ifndef online_judge // freopen ("in.txt ", "r", stdin); # endif int t, n; scanf ("% d", & T); While (t --) {scanf ("% d ", & N); For (INT I = 1; I <= N; I ++) {scanf ("% d", A + I);} memset (DP, -1, sizeof (DP); DP [1] [0] = 0; DP [1] [A [1] = A [1]; for (INT I = 2; I <= N; I ++) {for (Int J = 0; j <= 8191; j ++) {If (DP [(I-1) % 2] [J] =-1) {continue ;} DP [I % 2] [J] = max (DP [I % 2] [J], DP [(I-1) % 2] [J]); // If (J & (A [I]-1 )) {DP [I % 2] [A [I] = max (DP [I % 2] [A [I], DP [(I-1) % 2] [J] + A [I]); // case 1 continue;} int state, score; If (J & A [I]) {int TMP = J/A [I], K = 0; score = A [I]; while (TMP % 2) {k ++; tmp/= 2; score + = A [I] <K;} state = (TMP <k) * A [I]) | (A [I] <k ); DP [I % 2] [State] = max (DP [I % 2] [State], DP [(I-1) % 2] [J] + score ); // case 2 continue;} state = j | A [I]; score = A [I]; DP [I % 2] [State] = max (DP [I % 2] [State], DP [(I-1) % 2] [J] + score ); // case 3} int ans =-1; for (INT I = 0; I <8192; I ++) {ans = max (ANS, DP [n % 2] [I]);} printf ("% d \ n", ANS);} return 0 ;}
Zoj3162: Easy 2048 again (pressure DP)