SDUT 3222 Free Goodies(贪心+dp)

原创 2016年05月31日 10:13:00

Free Goodies

Time Limit: 1000MS Memory limit: 65536K

题目描述

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? 

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?

 

输入

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. 

输出

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.

示例输入

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

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
struct node
{
    int pi, ji;
    bool operator < (node a)const
    {
        return pi == a.pi ? ji < a.ji : pi > a.pi;
    }
}good[maxn];

int dp1[maxn][maxn], dp2[maxn][maxn];

int main()
{
    std::ios::sync_with_stdio(false);
    int T, n;
    char str[12];
    cin>>T;
    while(T-- && cin>>n>>str)
    {
        memset(dp1, 0, sizeof(dp1));
        memset(dp2, 0, sizeof(dp2));
        long long sum = 0;
        for(int i=1; i<=n; i++){
            cin>>good[i].pi>>good[i].ji;
            sum += good[i].pi;
        }
        sort(good+1, good+1+n);
        if(str[0] == 'J')
        {
            for(int i=1; i<=n; i++){
                for(int j=1; j<=(i+1)/2; j++){
                    if(dp1[i-1][j] > dp1[i-1][j-1] + good[i].ji){
                        dp1[i][j] = dp1[i-1][j];
                        dp2[i][j] = dp2[i-1][j];
                    } else if(dp1[i-1][j] == dp1[i-1][j-1] + good[i].ji){
                        dp1[i][j] = dp1[i-1][j];
                        dp2[i][j] = min(dp2[i-1][j], dp2[i-1][j-1]+good[i].pi);
                    } else{
                        dp1[i][j] = dp1[i-1][j-1] + good[i].ji;
                        dp2[i][j] = dp2[i-1][j-1] + good[i].pi;
                    }
                }
            }

            printf("%d %d\n", sum-dp2[n][(n+1)/2], dp1[n][(n+1)/2]);
        }
        else
        {
            for(int i=1; i<n; i++){
                for(int j=1; j<=(i+1)/2; j++){
                    if(dp1[i-1][j] > dp1[i-1][j-1] + good[i+1].ji){
                        dp1[i][j] = dp1[i-1][j];
                        dp2[i][j] = dp2[i-1][j];
                    } else if(dp1[i-1][j] == dp1[i-1][j-1] + good[i+1].ji){
                        dp1[i][j] = dp1[i-1][j];
                        dp2[i][j] = min(dp2[i-1][j], dp2[i-1][j-1]+good[i+1].pi);
                    } else{
                        dp1[i][j] = dp1[i-1][j-1] + good[i+1].ji;
                        dp2[i][j] = dp2[i-1][j-1] + good[i+1].pi;
                    }
                }
            }

            printf("%d %d\n", sum-dp2[n-1][n/2], dp1[n-1][n/2]);
        }
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

TZU2014年省赛个人热身赛1 3387:Free Goodies(DP+贪心)

描述 Petra and Jan have just received a box full of free goodies, and want to divide the goodies ...

uva 12260 Free Goodies DP+一点点贪心

Free Goodies Petra and Jan have just received a box full of free goodies, and want todivide the goo...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

LA 4945 Free Goodies(贪心)

题意:n个糖果,两个人 Petra,Jan,每个糖果对于这两个人都有一定的val值,Petra每次选对于自己val值最大的那个,如果一样,就选Jan的val值小的,Jan想让自己选的糖果的val值之和...

UVA 12260 Free Goodies (DP)

题意:有n个糖果,每个糖果有p,j两个值分别是Petra和Jan拿这糖果的开心值,现在有两个人Petra和Jan,Petra的取糖果方式是优先去p值大的,若有多个选j值小的;Jan取糖果的方式是尽量让...

第八届山东省赛 sdut 3903 CF(贪心+dp)

题意: 讲的是打cf的得分机制,给出n个题,每个题有它的初始分数a[i],随时间流逝要扣去的分数的系数d[i],做这道题需要花的时间c[i]。现在给你t时间,问你能获得的最大分数。 解题思路: 这...

贪心算法之子段和,山东省第八届acm大赛J题company,SDUT3902

company Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Descripti...

SDUT 3903 CF【Dp+排序】

CF Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description ...

SDUT 3061 聪明的玛雅 (状压DP)

题目地址:SDUT 3061 这题的比赛的时候的后台数据是错的。。。好坑啊。。。。就不吐槽出题人了。。 比赛的时候我的思路是错的,漏考虑了一种情况。应该把所有状态下的最短距离都要求出来,而我当时的...

【简单的线性DP】SDUT 3924 疯狂的bLue

疯狂的bLue Time Limit: 1000MS Memory Limit: 65536KB Problem Description 众所周知神秘的 ACM 实验室有一个史诗级的出题狂魔,...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)