HDU 4597 Play Game (DP,记忆化搜索,博弈)
2015-05-31 23:17
381 查看
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4597
大致题意:给出2堆数,Alice和Bob轮流拿,每次只能从每堆数的头或尾取,Alice先取,两人都采取最优策略,求Alice最后最多能拿的数的和。
思路:四维dp[ l1 ] [ r1 ] [ l2 ] [ r2 ],表示当前第一堆剩下的区间为(l1,r1),第二堆剩下的区间为(l2,r2),改状态下的最优答案,因为两人都要采取最优策略,所以dp转移的策略也是一样的,四个位置取最优值。
Code:
大致题意:给出2堆数,Alice和Bob轮流拿,每次只能从每堆数的头或尾取,Alice先取,两人都采取最优策略,求Alice最后最多能拿的数的和。
思路:四维dp[ l1 ] [ r1 ] [ l2 ] [ r2 ],表示当前第一堆剩下的区间为(l1,r1),第二堆剩下的区间为(l2,r2),改状态下的最优答案,因为两人都要采取最优策略,所以dp转移的策略也是一样的,四个位置取最优值。
Code:
/* W w w mm mm 222222222 7777777777777 */ /* W w w w m m m m 222 22 7777 */ /* w w w w m m m m 22 777 */ /* w w w w m m m m 22 77 */ /* w w w w m m m m 222 77 */ /* w w w w m m m m 222 77 */ /* w w w w m m m m 222 77 */ /* w w w w m m m m 222 77 */ /* w w w w m m m m 222 77 */ /* ww ww m mm m 222222222222222 77 */ //#pragma comment(linker, "/STACK:102400000,102400000") //C++ //int size = 256 << 20; // 256MB //char *p = (char*)malloc(size) + size; //__asm__("movl %0, %%esp\n" :: "r"(p)); //G++ #include<set> #include<map> #include<queue> #include<stack> #include<ctime> #include<deque> #include<cmath> #include<vector> #include<string> #include<cctype> #include<cstdio> #include<cstdlib> #include<cstring> #include<sstream> #include<iostream> #include<algorithm> #define REP(i,s,t) for(int i=(s);i<=(t);i++) #define REP2(i,t,s) for(int i=(t);i>=s;i--) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef unsigned long ul; const int N=25; int T; int n; int a ,b ; int dp ; int sum1 ,sum2 ; int dfs(int l1,int r1,int l2,int r2) { if(dp[l1][r1][l2][r2]!=-1) { return dp[l1][r1][l2][r2]; } if(l1>r1&&l2>r2) { return dp[l1][r1][l2][r2]=0; } int ans=0,sum=0; if(l1<=r1) { sum+=sum1[r1]-sum1[l1-1]; } if(l2<=r2) { sum+=sum2[r2]-sum2[l2-1]; } if(l1<=r1) { ans=max(ans,sum-dfs(l1+1,r1,l2,r2)); ans=max(ans,sum-dfs(l1,r1-1,l2,r2)); } if(l2<=r2) { ans=max(ans,sum-dfs(l1,r1,l2+1,r2)); ans=max(ans,sum-dfs(l1,r1,l2,r2-1)); } return dp[l1][r1][l2][r2]=ans; } int main() { #ifdef ONLINE_JUDGE #else freopen("test.in","r",stdin); #endif scanf("%d",&T); while(T--) { scanf("%d",&n); sum1[0]=sum2[0]=0; REP(i,1,n) { scanf("%d",&a[i]); sum1[i]=sum1[i-1]+a[i]; } REP(i,1,n) { scanf("%d",&b[i]); sum2[i]=sum2[i-1]+b[i]; } memset(dp,-1,sizeof(dp)); printf("%d\n",dfs(1,n,1,n)); } return 0; }
相关文章推荐
- 从《硅谷传奇》看微软和苹果
- 高性能 CSS3 动画
- uboot 2011-06 之顶层Makefile分析
- laravel 学习笔记——视图
- 产品设计(1) – 产品需求文档(PRD)介绍
- laravel 学习笔记——请求与响应
- uGUI动态加载控件位置错误
- VP8
- 在子线程中使用定时器,将定时器添加到RunLoop中
- hdu 5236
- HD1422重温世界杯
- 第十一周 项目四
- js之iframe子页面与父页面通信
- [Objective-C] 003_内存管理
- 随笔-即将崩溃的我
- 团队作业-第二周-SRS文档
- pv,uv,ip
- ALUA的Path Selection Policy选择
- timestamp的两个属性:CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP .
- 找数组最小值和次小值的方法