链接: http://soj.me/1176 也是老师让做的,总共六题;一串数字,两人取数,只能从两头取,A很聪明,就是你啦,B很笨,每次只取两端大的,现A用最优策略,B用最笨策略,问A能比B多多少分。 这题递归最合适了,记忆化搜索 #include<iostream> #include<cstring> using namespace std; int rem[1005][1005],arr[1005]; int max(int x, int y){ if(x>y) return x; else return y; } int db(int left, int right){ if(rem[left][right]!=0) return rem[left][right]; if(right-left==1){ return rem[left][right]=max(arr[left],arr[right]); } int x,y; if(arr[left+1]>=arr[right]) { rem[left+2][right]=db(left+2,right); x=rem[left+2][right]+arr[left]; } else{ rem[left+1][right-1]=db(left+1,right-1); x=rem[left+1][right-1]+arr[left]; } if(arr[right-1]>arr[left]) { rem[left][right-2]=db(left,right-2); y=rem[left][right-2]+arr[right]; } else { rem[left+1][right-1]=db(left+1,right-1); y=rem[left+1][right-1]+arr[right]; } return rem[left][right]=max(x,y); } int main(){ int a,count=0; while(cin>>a&&++count){ memset(rem,0,sizeof(rem)); int all=0; if(a==0) return 0; for(int i=0;i<a;++i) { cin>>arr[i]; all+=arr[i]; } cout<<"In game "<<count<<", the greedy strategy might lose by as many as "<<2*db(0,a-1)-all<<" points./n"; } return 0; }