「POJ1678」I Love this Game!
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.
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
题解
后面的人取的肯定要比前面的人取的要大,那么先排序,就可以依次取。
用f[m]表示如果先手取了第m个数,可以达到的最大分差。
那么最后一次取的分差就是本身。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define inf 0x7fffffff using namespace std; int t,n,num[10001],f[10001],a,b; int dp(int x) { int ans=inf; if(f[x]!=-1)return f[x]; for(int i=x+1;i<=n;i++) if(num[i]-num[x]>=a&&num[i]-num[x]<=b) ans=min(ans,num[x]-dp(i)); if(ans==inf)return f[x]=num[x]; return f[x]=ans; } int work() { int ans=-inf; for(int i=1;i<=n;i++) if(num[i]>=a&&num[i]<=b) ans=max(ans,dp(i)); return ans==-inf?0:ans; } int main() { scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&a,&b); for(int i=1;i<=n;i++) scanf("%d",&num[i]); memset(f,-1,sizeof(f)); sort(num+1,num+n+1); printf("%d\n",work()); } return 0; } |