Petra and Jan have just received a box full of free goodies, and want to divide the goodies between them. However, it is not easy to do this fairly, since they both value different goodies differently.
To divide the goodies, they have decided upon the following procedure: they choose goodies one by one, in turn, until all the goodies are chosen. A coin is tossed to decide who gets to choose the first goodie. Petra and Jan have different strategies in deciding what to choose. When faced with a choice, Petra always selects the goodie that is most valuable to her. In case of a tie, she is very considerate and picks the one that is least valuable to Jan. (Since Petra and Jan are good friends, they know exactly how much value the other places on each goodie.) Jan's strategy, however, consists of maximizing his own final value. He is also very considerate, so if multiple choices lead to the same optimal result, he prefers Petra to have as much final value as possible. You are given the result of the initial coin toss. After Jan and Petra have finished dividing all the goodies between themselves, what is the total value of the goodies each of them ends up with?Input
On the first line a positive integer: the number of test cases, at most 100. After that per test case:- One line with an integer n (1 ≤ n ≤ 1 000): the number of goodies.
- One line with a string, either "Petra" or "Jan": the person that chooses first.
- n lines with two integers pi and ji (0 ≤ pi,ji ≤ 1 000) each: the values that Petra and Jan assign to the i-th goodie, respectively.
Output
Per test case:- One line with two integers: the value Petra gets and the value Jan gets. Both values must be according to their own valuations.
Sample in- and output
Input | Output |
3 4 Petra 100 80 70 80 50 80 30 50 4 Petra 10 1 1 10 6 6 4 4 7 Jan 4 1 3 1 2 1 1 1 1 2 1 3 1 4 | 170 130 14 16 9 10 |
题意:两个人分硬币,每个硬币对两个人的价值可能不同,Petra每次会取当前对自己价值最大的,如果有多解,取对Jan价值最小的,Jan每次会采取对自己最后的价值最大的方式,如果有多解,取对Petra最后结果价值最大的,问最后取的结果。
思路:先按照Petra取的顺序排序,然后dp[i][j]表示前i个硬币中Petra取走j个的时候,两个人当前硬币的情况,然后转移时考虑某些非法情况,不能转移。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int a,b;
}coin[1010];
bool cmp(node a,node b)
{
if(a.a==b.a)
return a.b<b.b;
return a.a>b.a;
}
char s[20];
int dp[2][1010][2];
int main()
{
int T,t,n,i,j,k,a,b;
scanf("%d",&T);
for(t=1;t<=T;t++)
{
scanf("%d",&n);
scanf("%s",s);
if(s[0]=='P')
k=0;
else
k=1;
for(i=1;i<=n;i++)
scanf("%d%d",&coin[i].a,&coin[i].b);
sort(coin+1,coin+1+n,cmp);
for(i=0;i<=n;i++)
dp[0][i][0]=-1;
dp[0][0][0]=0;dp[0][0][1]=0;
for(i=1;i<=n;i++)
{
if(i&1)
a=0,b=1;
else
a=1,b=0;
for(j=0;j<=n;j++)
dp[b][j][0]=-1;
for(j=0;j<=i;j++)
if(i-j<=j+k)
{
if(dp[a][j][0]==-1 && (j==0 || dp[a][j-1][0]==-1))
continue;
if(dp[a][j][0]==-1)
{
dp[b][j][0]=dp[a][j-1][0]+coin[i].a;
dp[b][j][1]=dp[a][j-1][1];
}
else if(j==0 || dp[a][j-1][0]==-1)
{
dp[b][j][0]=dp[a][j][0];
dp[b][j][1]=dp[a][j][1]+coin[i].b;
}
else if(dp[a][j-1][1]>dp[a][j][1]+coin[i].b ||
(dp[a][j-1][1]==dp[a][j][1]+coin[i].b && dp[a][j-1][0]+coin[i].a>=dp[a][j][0])
)
{
dp[b][j][0]=dp[a][j-1][0]+coin[i].a;
dp[b][j][1]=dp[a][j-1][1];
}
else
{
dp[b][j][0]=dp[a][j][0];
dp[b][j][1]=dp[a][j][1]+coin[i].b;
}
//printf("%d %d %d %d\n",i,j,dp[b][j][0],dp[b][j][1]);
}
}
if(n&1)
{
if(k==0)
k=n/2+1;
else
k=n/2;
}
else
k=n/2;
printf("%d %d\n",dp[b][k][0],dp[b][k][1]);
}
}