uva12260 - Free Goodies 贪心+dp

来源:互联网 发布:linux 重启服务器命令 编辑:程序博客网 时间:2024/06/03 16:47

B  Free Goodies

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

InputOutput
34Petra100 8070 8050 8030 504Petra10 11 106 64 47Jan4 13 12 11 11 21 31 4
170 13014 169 10

  两个人轮流拿糖果,对于某个糖果如果petra拿了得到权值x,jan拿了得到权值y。petra的选择是拿当前x最大的,如果有多个拿y最小的,jan的选择是使他最后得到的权值最多,在他得到最多的情况下使petra也尽量多。这题参考了题解。

  把这些糖果按x从大到小排序,x相同时y从小到大。那么petra一定是按照这个顺序拿,如果petra先拿,那么前n个里面jan最多拿n/2个,用dp[i][j]表示jan前i个里面拿j个,j<=i/2,那么dp[i][j]=min(dp[i-1][j],dp[i-1][j-1]+y[i] | 当dp[i-1][j-1]存在),dp2[i][j]表示前i个里面jan拿掉j个petra失去的权值,越小越好,在维护dp的时候同时维护dp2就行了。

  注意如果是jan先,那么前i个他可以拿掉(i+1)/2个。

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;typedef long long LL;const int MAXN=1010;const int MAXNODE=MAXN*4;const LL MOD=1000000007;int T,N;int dp[MAXN][MAXN/2],dp2[MAXN][MAXN/2];char str[10];struct St{    int x,y;    bool operator < (const St& rhs) const{        if(x==rhs.x) return y<rhs.y;        return x>rhs.x;    }}a[MAXN];int main(){    freopen("in.txt","r",stdin);    scanf("%d",&T);    while(T--){        scanf("%d%s",&N,str);        int sum=0;        for(int i=1;i<=N;i++){            scanf("%d%d",&a[i].x,&a[i].y);            sum+=a[i].x;        }        sort(a+1,a+1+N);        memset(dp,-1,sizeof(dp));        memset(dp2,0,sizeof(dp2));        for(int i=0;i<=N;i++) dp[i][0]=0;        int n=str[0]=='P'?0:1;        for(int i=1;i<=N;i++){            n++;            for(int j=1;j<=n/2;j++){                dp[i][j]=dp[i-1][j];                dp2[i][j]=dp2[i-1][j];                if(dp[i-1][j-1]!=-1){                    if(dp[i-1][j-1]+a[i].y>dp[i][j]){                        dp[i][j]=dp[i-1][j-1]+a[i].y;                        dp2[i][j]=dp2[i-1][j-1]+a[i].x;                    }                    else if(dp[i-1][j-1]+a[i].y==dp[i][j]&&dp2[i-1][j-1]+a[i].x<dp2[i][j]) dp2[i][j]=dp2[i-1][j-1]+a[i].x;                }            }        }        printf("%d %d\n",sum-dp2[N][n/2],dp[N][n/2]);    }    return 0;}



0 0
原创粉丝点击