USACO3.35A Game(记忆化)
刚开始理解有点误,想成每步都是最优的 ,结果卡在第六组数据上,,
无奈瞧了下别人的 发现自己理解错了,,我看的还是中文翻译。。
简单的记忆化
1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: game1 5 */ 6 #include <iostream> 7 #include<cstdio> 8 #include<algorithm> 9 #include<stdlib.h> 10 #include<cstring> 11 using namespace std; 12 int a[210],dp[210][210],s[210]; 13 int find(int i,int j) 14 { 15 16 if(i==j) 17 { 18 //if(i==2) 19 // cout<<a[i]<<endl; 20 return a[i]; 21 } 22 if(dp[i][j]) 23 return dp[i][j]; 24 dp[i][j] = max(a[i]+s[j]-s[i]-find(i+1,j),a[j]+s[j-1]-s[i-1]-find(i,j-1)); 25 return dp[i][j]; 26 } 27 int main() 28 { 29 freopen("game1.in","r",stdin); 30 freopen("game1.out","w",stdout); 31 int i,n,sum=0; 32 cin>>n; 33 for(i = 1; i <= n ;i++) 34 { 35 cin>>a[i]; 36 s[i] = s[i-1]+a[i]; 37 sum+=a[i]; 38 } 39 int ss = find(1,n); 40 cout<<ss<<" "<<sum-ss<<endl; 41 return 0; 42 }