UVA 10891 Game of Sum(区间博弈dp)***
2016-10-03 11:04
399 查看
首先是博弈的思想,用两个for循环枚举给对手剩下的子序列,然后对手取的也是最优,这时候返回的值就是对手最优的值,但是如果对手取的值是正的,那么表明我再取能把对手取的正值放到我这里,我能够更优,直到对手最优只能是负值时,如果对手无论如何最优不能取负值,那么我就把序列全部取完,我一定是最优的,其本质还是极大极小搜索。
动态规划
// // main.cpp // Richard // // Created by 邵金杰 on 16/10/3. // Mail:1016427040@qq.com or jasonshaosjj@gmail.com // Copyright © 2016年 邵金杰. All rights reserved. // #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=100+10; int a[maxn],s[maxn],vis[maxn][maxn],d[maxn][maxn]; int dp(int i,int j) { if(vis[i][j]==1) return d[i][j]; vis[i][j]=1; int m=0; for(int k=i+1;k<=j;k++) m=min(m,dp(k,j)); for(int k=i;k<j;k++) m=min(m,dp(i,k)); d[i][j]=s[j]-s[i-1]-m; return d[i][j]; } int main() { int n; while(scanf("%d",&n)&&n) { for(int i=1;i<=n;i++) {scanf("%d",&a[i]);s[i]=s[i-1]+a[i];} memset(vis,0,sizeof(vis)); int ans=dp(1,n); printf("%d\n",2*ans-s ); } return 0; }
动态规划
// // main.cpp // Richard // // Created by 邵金杰 on 16/10/3. // Mail:1016427040@qq.com or jasonshaosjj@gmail.com // Copyright © 2016年 邵金杰. All rights reserved. // #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=100; int dp[maxn][maxn],f[maxn][maxn],g[maxn][maxn],s[maxn],a[maxn]; int main() { int n; while(scanf("%d",&n)&&n) { for(int i=1;i<=n;i++) {scanf("%d",&a[i]);s[i]=s[i-1]+a[i];} for(int i=1;i<=n;i++) f[i][i]=g[i][i]=dp[i][i]=a[i]; for(int L=1;L<n;L++) { for(int i=1;i<=n;i++) { int m=0; int j=i+L; m=min(m,min(f[i+1][j],g[i][j-1])); dp[i][j]=s[j]-s[i-1]-m; f[i][j]=min(f[i+1][j],dp[i][j]); g[i][j]=min(g[i][j-1],dp[i][j]); } } cout<<2*dp[1] -s <<endl; } return 0; }
相关文章推荐
- uva 10891 - Game of Sum(博弈,区间dp)
- UVa 10891 Game of Sum(经典博弈区间DP)
- UVa 10891 Game of Sum (区间DP&博弈)
- uva10891 Game of Sum 博弈区间dp
- UVa 10891 Game of Sum(博弈区间DP)
- Uva 10891 Game of Sum(区间博弈dp)
- uva_10891 - Game of Sum( 博弈区间DP )
- UVA 10891 Game of Sum(经典区间dp)
- UVA 10891 Game of Sum(区间DP)
- UVA 10891 Game of Sum(区间DP)
- UVA 10891 Game of Sum(区间DP)
- UVA 10891 Game of Sum 博弈DP -
- UVA 10891 Game of Sum(区间DP)
- UVa UVA 10891 Game of Sum (区间DP)
- Uva 10891 Game of Sum(区间DP)
- UVA 10891 Game of Sum(区间DP)
- UVA 10891 Game of Sum(dp博弈)
- UVA - 10891 Game of Sum(记忆化搜索 区间dp)
- UVA 10891 - Game of Sum(区间dp)
- UVA 10891 Game of Sum(区间DP)