I Love this Game!
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 1363 | Accepted: 505 |
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
Source
题意:给n个数,并给一个区间[a,b],第一次选的数要在这个区间内,然后每次选n个数中的某个数,使得这个数减去对手所选的数得到的差值在[a,b]区间内。
要求第一个选手所选的数的和减去第二个人所选的数的和的最大差值。
a>0,很明显每次选的数只可能比上次所选的数大,所以把这n个数进行排序。
dp[i]表示选择i位置的数所能得到的最大差值。
//dp[i] 选择能得到的最大分差
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int m=100005;
const int inf=999999;
int dp[m],num[m];
int n,a,b;
int dfs(int x)
{
if(dp[x]!=-inf) return dp[x];
int ans=inf;
for(int i=x+1;i<n;i++)
{
int c=num[i]-num[x];
if(c>=a&&c<=b)
{
ans=min(ans,num[x]-dfs(i));
}
}
if(ans==inf) return dp[x]=num[x];
else
return dp[x]=ans;
}
void solve()
{
int ans=-inf;
for(int i=0;i<n;i++)
{
if(num[i]<=b&&num[i]>=a)
{
ans=max(ans,dfs(i));
}
}
if(ans==-inf) puts("0");
else
cout<<ans<<endl;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&a,&b);
for(int i=0;i<n;i++)
{
dp[i]=-inf;
scanf("%d",&num[i]);
}
sort(num,num+n);
solve();
}
}