zoukankan      html  css  js  c++  java
  • [hdu5375 Gray code]DP

    题意:给一个二进制码,其中有一些位上为'?',对每个问号确定是'0'还是'1',最后以它对应的格雷码来取数,第i位为1则取第i个数,求取得的数的和的最大值。

    思路:二进制码B转换成格雷码G的方法是,Gi=Bi^Bi+1,Gn=Bn。所以第i位如果为'?',那么选'1'还是'0'只会影响邻位,于是用dp即可解决。

    #include <map>
    #include <set>
    #include <cmath>
    #include <ctime>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    #define X                   first
    #define Y                   second
    #define pb                  push_back
    #define mp                  make_pair
    #define all(a)              (a).begin(), (a).end()
    #define fillchar(a, x)      memset(a, x, sizeof(a))
    #define copy(a, b)          memcpy(a, b, sizeof(a))
    
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef unsigned long long ull;
    
    //#ifndef ONLINE_JUDGE
    void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);}
    void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
    void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1;
    while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
    void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
    void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
    void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
    //#endif
    template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
    template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
    template<typename T>
    void V2A(T a[],const vector<T>&b){for(int i=0;i<b.size();i++)a[i]=b[i];}
    template<typename T>
    void A2V(vector<T>&a,const T b[]){for(int i=0;i<a.size();i++)a[i]=b[i];}
    
    const double PI = acos(-1.0);
    const int INF = 1e9 + 7;
    const double EPS = 1e-8;
    
    /* -------------------------------------------------------------------------------- */
    
    const int maxn = 2e5 + 7;
    
    int dp[maxn][2], a[maxn];
    char s[maxn];
    int main() {
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
    #endif // ONLINE_JUDGE
        int T, cas = 0;
        cin >> T;
        while (T --) {
            scanf("%s", s);
            int n = strlen(s);
            for (int i = 0; i < n; i ++) scanf("%d", a + i);
            fillchar(dp, 0);
            for (int i = n - 2; i >= 0; i --) {
                if (s[i] == '0') {
                    if (s[i + 1] == '0') dp[i][0] = dp[i + 1][0];
                    if (s[i + 1] == '1') dp[i][0] = dp[i + 1][1] + a[i + 1];
                    if (s[i + 1] == '?') dp[i][0] = max(dp[i + 1][0], dp[i + 1][1] + a[i + 1]);
                }
                if (s[i] == '1') {
                    if (s[i + 1] == '0') dp[i][1] = dp[i + 1][0] + a[i + 1];
                    if (s[i + 1] == '1') dp[i][1] = dp[i + 1][1];
                    if (s[i + 1] == '?') dp[i][1] = max(dp[i + 1][0] + a[i + 1], dp[i + 1][1]);
                }
                if (s[i] == '?') {
                    if (s[i + 1] == '0') {
                        dp[i][0] = dp[i + 1][0];
                        dp[i][1] = dp[i + 1][0] + a[i + 1];
                    }
                    if (s[i + 1] == '1') {
                        dp[i][0] = dp[i + 1][1] + a[i + 1];
                        dp[i][1] = dp[i + 1][1];
                    }
                    if (s[i + 1] == '?') {
                        dp[i][0] = max(dp[i + 1][0], dp[i + 1][1] + a[i + 1]);
                        dp[i][1] = max(dp[i + 1][0] + a[i + 1], dp[i + 1][1]);
                    }
                }
            }
            int ans;
            if (s[0] == '0') ans = dp[0][0];
            if (s[0] == '1') ans = dp[0][1] + a[0];
            if (s[0] == '?') ans = max(dp[0][0], dp[0][1] + a[0]);
            printf("Case #%d: %d
    ", ++ cas, ans);
        }
        return 0;
    }
    
  • 相关阅读:
    24-反转链表
    23-链表中环的入口节点
    22-链表中倒数第k个节点
    21-调整数组顺序使奇数位于偶数前面
    18-删除链表的节点
    17-打印从1到最大的n位数
    16-数值的整数次方
    15-二进制中1的个数
    14-剪绳子
    13-机器人的运动范围
  • 原文地址:https://www.cnblogs.com/jklongint/p/4724144.html
Copyright © 2011-2022 走看看