POJ 1678 I Love this Game!#dp博弈
http://poj.org/problem?id=1678
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int INF=10e8; int n,a,b,p[10005],dp[10005]; int dfs(int x) { if(dp[x]!=-INF) return dp[x]; int ans=INF; for(int i=x+1;i<n;i++) if(p[i]-p[x]>=a&&p[i]-p[x]<=b)//对方要尽量取差值大的,那么要得到差值尽可能小的 ans=min(ans,p[x]-dfs(i)); //== ans=max(ans,dfs(i)); ans=p[x]-ans; if(ans==INF) return dp[x]=p[x]; return dp[x]=ans; } int op() { int ans=-INF; for(int i=0;i<n;i++) if(p[i]>=a&&p[i]<=b) ans=max(ans,dfs(i)); return ans==-INF?0:ans; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&a,&b); for(int i=0;i<n;i++) { scanf("%d",&p[i]); dp[i]=-INF; } sort(p,p+n); printf("%d\n",op()); } return 0; }