Easy 2048 Again(狀壓dp)

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   ar   div   

題目連結:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3802

題意: 從數列A中, 刪除若干個數(可以0個), 是刪除後的數列, 進行類似  Flappy 2048 遊戲的運算, 使結果最大, 求該最大值。

 

題解: 每個數ai, 取或不取, 滿足 可用二進位表示。 當 ai < a[i + 1] 是, 易想到 如果兩個數都取, ai 將不會在合并。 所以只需考慮ai[i + 1] 前的 降序列即可 .如果沒個數都為16, 合并後的數列的最大值為4026 = 2^12。 也就是說, a[i + 1]最多隻需考慮 合并到a[i +1]為後的前 12項。

具體的看 代碼吧

 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(a) memset(a, 0, sizeof(a))18 #define Neg(a)  memset(a, -1, sizeof(a))19 #define All(a) a.begin(), a.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]; // 滾動數組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         ans = 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 記錄每次cal 時, 改動的最大值。 (不知為什麼, 沒最佳化這一步, 逾時, 最佳化後 10ms,相差好大)43         if ((i & (v - 1))) { 44             tmpmx = max(tmpmx, arr[i]);  // 記錄 arr[i + 1] > arr[i]中的多種情況 下的最大得分。45             continue;46         }47         if (arr[i] && arr[i + v] < arr[i] + (tmpv = add(i,v))) { // 計算並更新arr[i + 1] <= arr[i]中多種情況48             arr[i + v] = arr[i] + tmpv;49             mx = max(i + v, mx);         // 記錄該次運行時所用的最大 數。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;         //第一次的範圍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(狀壓dp)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.