ZOJ 3802Easy 2048 Again(状压DP)

来源:互联网 发布:大数据修仙 起点 编辑:程序博客网 时间:2024/06/07 20:58

一开始没有想到加完之后的结果是可以直接加出来的,过程中的常数很高。

也完全没有想到数组开大会TLE,最后还是看了别人的代码对拍才用了滚动数组。

总的来说  非常好的状压dp吧


#include <cstdio>  #include <cstring>  #include <algorithm>  #include<iostream>#include<queue>#include<map>#include<cmath>using namespace std;const int maxn = 57;#define MAXN 100005#define ll long longint dp[2][(1 << 14)];int A[550];int main(){int T;cin >> T;while (T--){int n;cin >> n;memset(dp, -1, sizeof dp);for (int i = 1; i <= n; i++){scanf("%d", &A[i]);}int pos = 1;int ans = 0;dp[0][0] = 0;for (int i = 1; i <= n; i++){for (int j = 0; j<4096 * 2; j++){if (dp[pos ^ 1][j] == -1) continue;dp[pos][j] = max(dp[pos][j], dp[pos ^ 1][j]);ans = max(ans, dp[pos][j]);int t = j;int q = A[i] - 1;int sum = A[i];if ((t&q) == 0){int k = A[i];while ((t&k)){sum += k << 1;k <<= 1;}t &= ~(k - 1);t |= k;}else t = A[i];dp[pos][t] = max(dp[pos][t], dp[pos ^ 1][j] + sum);ans = max(ans, dp[pos][t]);}pos ^= 1;}printf("%d\n", ans);}return 0;}


0 0
原创粉丝点击