zoukankan      html  css  js  c++  java
  • HDU 4597 Play Game 记忆化DP

    Play Game

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)


    Problem Description
    Alice and Bob are playing a game. There are two piles of cards. There are N cards in each pile, and each card has a score. They take turns to pick up the top or bottom card from either pile, and the score of the card will be added to his total score. Alice and Bob are both clever enough, and will pick up cards to get as many scores as possible. Do you know how many scores can Alice get if he picks up first?
     
    Input
    The first line contains an integer T (T≤100), indicating the number of cases. 
    Each case contains 3 lines. The first line is the N (N≤20). The second line contains N integer ai (1≤ai≤10000). The third line contains N integer bi (1≤bi≤10000).
     
    Output
    For each case, output an integer, indicating the most score Alice can get.
     
    Sample Input
    2
    1 2
    3 5
    3
    3 10 100
    20 2 4 3
     
    Sample Output
    53
    105
     
    Source
     
     
       没有后效性,因为每一状态都是最优策略。
       确定状态的转移 。每个状态的数的和一定,一旦子问题取数和确定,则父节点取树和确定,枚举4中情况,取最大(优)。
       此题递归树性质:
              父亲节点father 的取数和 与  儿子节点son的取数和 为 定值 。这就是状态转移的关键地方 。 
    #include <iostream>
    #include <string>
    #include <string.h>
    #include <map>
    #include <stdio.h>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <math.h>
    #include <set>
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std ;
    int a[28] ,b[28] ;
    int suma[28] ,sumb[28] ;
    int dp[28][28][28][28] ;
    int N ;
    
    int dfs(int U , int B ,int L ,int R){
        if(dp[U][B][L][R] != -1)
          return dp[U][B][L][R] ;
        if(U > B && L > R)
          return dp[U][B][L][R] = 0 ;
        int sum = 0 ;
        if(U <= B)
           sum += suma[B] - suma[U-1] ;
        if(L <= R)
           sum += sumb[R] - sumb[L-1] ;
        int nowstate = 0 ;
        if(U == B)
           nowstate = Max(nowstate , sum - dfs(U+1,B-1,L,R)) ;
        else if(U < B){
           nowstate = Max(nowstate , sum - dfs(U+1,B,L,R)) ;
           nowstate = Max(nowstate , sum - dfs(U,B-1,L,R)) ;
        }
        if(L == R)
           nowstate = Max(nowstate , sum - dfs(U,B,L+1,R-1)) ;
        else if(L < R){
           nowstate = Max(nowstate , sum - dfs(U,B,L+1,R)) ;
           nowstate = Max(nowstate , sum - dfs(U,B,L,R-1)) ;
        }
        return dp[U][B][L][R] = nowstate ;
    }
    
    int main(){
       int T ;
       scanf("%d",&T) ;
       while(T--){
           scanf("%d",&N) ;
           suma[0] = sumb[0] = 0 ;
           for(int i = 1 ; i <= N ; i++){
               scanf("%d",&a[i]) ;
               suma[i] = suma[i-1] + a[i] ;
           }
           for(int i = 1 ; i <= N ; i++){
               scanf("%d",&b[i]) ;
               sumb[i] = sumb[i-1] + b[i] ;
           }
           memset(dp,-1,sizeof(dp)) ;
           printf("%d
    ",dfs(1,N,1,N)) ;
       }
       return 0 ;
    }
     
  • 相关阅读:
    Redis学习03:Redis数据持久化
    Redis学习02:Redis数据类型
    项目总结42:Linux下vim操作常用指令
    Java深入学习02:CAS 算法以及低层原理
    Java深入学习01:volatile的原理和使用
    项目总结39:Springboot打jar包并本地运行
    项目总结38:使用httpcomponents: httpclient请求数据
    项目总结37:Java上传图片保存到Oracle以及读取图片
    Redis学习01:Redis配置文件redis.conf详解
    Oracle学习笔记04:timestamp时间大小比较
  • 原文地址:https://www.cnblogs.com/liyangtianmen/p/3462513.html
Copyright © 2011-2022 走看看