Zoj 3802 easy 2048 again pressure DP

Source: Internet
Author: User

Directly from the past to the next DP, because there are only 500 numbers in total, so the total number of tickets will not exceed 4096, and because it is a flappy 2048 rule, therefore, only a string at the end of the subsequent series is effective, so it can be squashed.

1700 Ms =, it is said that it would be much better to optimize with a rolling Array

#include <cstdio>#include <cstring>#include <iostream>#include <map>#include <set>#include <vector>#include <string>#include <queue>#include <deque>#include <bitset>#include <list>#include <cstdlib>#include <climits>#include <cmath>#include <ctime>#include <algorithm>#include <stack>#include <sstream>#include <numeric>#include <fstream>#include <functional>using namespace std;#define MP make_pair#define PB push_backtypedef long long LL;typedef unsigned long long ULL;typedef vector<int> VI;typedef pair<int,int> pii;const int INF = INT_MAX / 3;const double eps = 1e-8;const LL LINF = 1e17;const double DINF = 1e60;const int maxn = 505;const int maxs = (4096 + 5);int num[maxn],n;int f[maxn][maxs];inline int lowbit(int x) {    return x & -x;}void solve() {    memset(f,-1,sizeof(f));    f[0][0] = 0;    int ans = 0;    for(int i = 1;i <= n;i++) {        for(int j = 0;j < 4096;j++) if(f[i - 1][j] != -1) {            int nownum = num[i], nowst = j, nowval = nownum;            f[i][j] = max(f[i][j], f[i - 1][j]);            while(lowbit(nowst) == (nownum >> 1)) {                nowval += (nownum << 1);                nowst ^= (nownum >> 1);                nownum <<= 1;            }            if(lowbit(nowst) < (nownum >> 1)) nowst = 0;            nowst |= (nownum >> 1);            f[i][nowst] = max(f[i][nowst],f[i - 1][j] + nowval);        }    }    for(int i = 0;i < 4096;i++) ans = max(ans,f[n][i]);    printf("%d\n",ans);}int main() {    int T; scanf("%d",&T);    while(T--) {       scanf("%d",&n);       for(int i = 1;i <= n;i++) {           scanf("%d",&num[i]);       }       solve();    }    return 0;}

 

Zoj 3802 easy 2048 again pressure DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.