I Love this Game!
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 1560 | Accepted: 602 |
Description
A traditional game is played between two players on a pool of n numbers (not necessarily distinguishing ones).
The first player will choose from the pool a number x1 lying in [a, b] (0 < a < b), which means a <= x1 <= b. Next the second player should choose a number y1 such that y1 - x1 lies in [a, b] (Attention! This implies y1 > x1 since a > 0). Then the first player should choose a number x2 such that x2 - y1 lies in [a, b]... The game ends when one of them cannot make a choice. Note that a player MUST NOT skip his turn.
A player's score is determined by the numbers he has chose, by the way:
player1score = x1 + x2 + ...
player2score = y1 + y2 + ...
If you are player1, what is the maximum score difference (player1score - player2score) you can get? It is assumed that player2 plays perfectly.
The first player will choose from the pool a number x1 lying in [a, b] (0 < a < b), which means a <= x1 <= b. Next the second player should choose a number y1 such that y1 - x1 lies in [a, b] (Attention! This implies y1 > x1 since a > 0). Then the first player should choose a number x2 such that x2 - y1 lies in [a, b]... The game ends when one of them cannot make a choice. Note that a player MUST NOT skip his turn.
A player's score is determined by the numbers he has chose, by the way:
player1score = x1 + x2 + ...
player2score = y1 + y2 + ...
If you are player1, what is the maximum score difference (player1score - player2score) you can get? It is assumed that player2 plays perfectly.
Input
The first line contains a single integer t (1 <= t <= 20) indicating the number of test cases. Then follow the t cases. Each case contains exactly two lines. The first line contains three integers, n, a, b (2 <= n <= 10000, 0 < a < b <= 100); the second line contains n integers, the numbers in the pool, any of which lies in [-9999, 9999].
Output
For each case, print the maximum score difference player1 can get. Note that it can be a negative, which means player1 cannot win if player2 plays perfectly.
Sample Input
3 6 1 2 1 3 -2 5 -3 6 2 1 2 -2 -1 2 1 2 1 0
Sample Output
-3 0 1
记义化搜索,因为,我们用dp[i]表示先手搜到第i个是,能得到的最大值,那么对于任意状态都用dp[i]=pri[i]-max{dp[j]};相减一下,刚好就是,把先手后手交换了,可以用一个式子,统一表示,j是i的后继结点,这样,就可以得出结果了!
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define M 10050 #define inf 0x4f4f4f4f int pri[M],a,b,n,dp[M]; int dfs(int now){ if(dp[now]!=-inf)return dp[now]; int ans=-inf,k; for(int i=now+1;i<n;i++){ int temp=pri[i]-pri[now]; if(temp>b)break; if(temp>=a&&(k=dfs(i))>ans){ ans=k; } } if(ans==-inf)ans=0; return dp[now]=pri[now]-ans; } int main() { int tcase,i,j,k; scanf("%d",&tcase); while(tcase--){ scanf("%d%d%d",&n,&a,&b); for(i=0;i<n;i++) dp[i]=-inf; for(i=0;i<n;i++) scanf("%d",&pri[i]); sort(pri,pri+n); for(i=1,j=0;i<n;i++){ if(pri[i]!=pri[j])pri[++j]=pri[i]; } int ans=-inf; for(i=0;i<n;i++){ if(pri[i]<=b&&pri[i]>=a&&(k=dfs(i))>=ans){ ans=k; } } if(ans==-inf)printf("0\n"); else printf("%d\n",ans); } return 0; }