Question link: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3802.
From column A, delete a number (which can be 0). It is the deleted Number of columns. perform computation similar to flappy 2048 to maximize the result.
Question: Each number of AI instances can be expressed in binary format if they are obtained or not obtained. When AI <A [I + 1] is, it is easy to think that if both numbers are obtained, AI will not be merged. Therefore, you only need to consider the descending sequence before Ai [I + 1]. If no number is 16, the maximum value of the merged series is 4026 = 2 ^ 12. That is to say, a [I + 1] only needs to consider merging to the first 12 items after a [I + 1.
For details, see the code.
1/*** good luck ***/2 # DEFINE _ crt_secure_no_warnings 3 # include <iostream> 4 # include <cstdio> 5 # include <cstdlib> 6 # include <cstring> 7 # include <string> 8 # include <algorithm> 9 # include <stack> 10 # include <map> 11 # include <queue> 12 # include <vector> 13 # include <set> 14 # include <functional> 15 # include <cmath> 16 17 # define zero () memset (A, 0, sizeof (A) 18 # define neg (a) memset (A,-1, sizeof ()) 19 # define all (A). begin (),. end () 20 # define Pb push_back21 # define INF 0x3f3f3f3f22 # define inf2 0x7fffffffffffffff23 # define ll long long24 using namespace STD; 25 // # pragma comment (linker, "/Stack: 102400000,102400000 ") 26 27 const int Mw = 1 <13; 28 int N; 29 int arr [MW]; // scroll array 30 int MX; 31 inline int add (int I, int v) {32 int TMP = (I + v) | I)-I; 33 int ans = V; 34 while (v <TMP) {35 S = ans + (v <= 1); 36} 37 return ans; 38} 39 void CAL (INT v) {40 int tmpv; 41 int tmpmx = 0; 42 for (INT I = mx; I> = 0; -- I) {// MX records the maximum value of each change to Cal. (I do not know why, this step is not optimized, timeout, 10 ms after optimization, great difference) 43 If (I & (V-1) {44 tmpmx = max (tmpmx, arr [I]); // records the maximum score of ARR [I + 1]> arr [I] in multiple situations. 45 continue; 46} 47 If (ARR [I] & arr [I + V] <arr [I] + (tmpv = add (I, V ))) {// calculate and update arr [I + 1] <= arr [I] in multiple cases 48 arr [I + V] = arr [I] + tmpv; 49 MX = max (I + V, MX); // record the maximum number used for this operation. 50} 51} 52 arr [v] = V + tmpmx; 53 MX = max (v, MX); 54} 55 56 int main () {57 int T; 58 scanf ("% d", & T); 59 While (t --) {60 zero (ARR); 61 int val = 0; 62 scanf ("% d ", & N); 63 scanf ("% d", & Val); 64 MX = val; // The first time range is 65 CAL (VAL); 66 for (INT I = 2; I <= N; ++ I) {67 scanf ("% d", & Val); 68 CAL (VAL); 69} 70 int ans = 0; 71 for (INT I = 0; I <MW; ++ I) ans = max (ANS, arr [I]); 72 printf ("% d \ n ", ans); 73} 74 return 0; 75}
Easy 2048 again (pressure DP)