标签:des style blog http java color
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 630 Accepted Submission(s): 374
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 const int N = 22; 7 int _, n, dp[N][N][N][N]; 8 int a[N], b[N], suma[N], sumb[N]; 9 10 int DP(int al, int ar, int bl, int br, int flag)//flag为1表示先挑, flag为0表示后挑 11 { 12 if(al>ar&&bl>br) return 0; 13 if(dp[al][ar][bl][br]){ 14 if(flag) return dp[al][ar][bl][br]; 15 else return suma[ar] - suma[al-1] + sumb[br] - sumb[bl-1]-dp[al][ar][bl][br]; 16 } 17 int ans = 0; 18 if(al<=ar){ 19 ans = max(DP(al+1, ar, bl, br, 0) + a[al], ans); 20 ans = max(DP(al, ar-1, bl, br, 0) + a[ar], ans); 21 } 22 if(bl<=br){ 23 ans = max(DP(al, ar, bl+1, br, 0) + b[bl], ans); 24 ans = max(DP(al, ar, bl, br-1, 0) + b[br], ans); 25 } 26 dp[al][ar][bl][br] = ans; 27 if(!flag) ans = suma[ar] - suma[al-1] + sumb[br] - sumb[bl-1] - ans;//如果是后挑,把剩余卡的综合减去先挑者能得到的最大分数 28 return ans; 29 } 30 31 void solve() 32 { 33 memset(dp, 0, sizeof(dp)); 34 scanf("%d", &n); 35 for(int i=1; i<=n; i++) { 36 scanf("%d", a+i); 37 suma[i] = suma[i-1] + a[i]; 38 } 39 for(int i=1; i<=n; i++) { 40 scanf("%d", b+i); 41 sumb[i]= sumb[i-1] + b[i]; 42 } 43 printf("%d\n", DP(1, n, 1, n, 1)); 44 } 45 46 int main() 47 { 48 // freopen("in.txt", "r", stdin); 49 scanf("%d", &_); 50 while(_--) solve(); 51 return 0; 52 }
hdu 4597 Play Game,布布扣,bubuko.com
标签:des style blog http java color
原文地址:http://www.cnblogs.com/zyx1314/p/3843834.html