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;
}


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

DP与贪心算法的一点浅见

DP:考虑且仅仅考虑由前一阶段状态转移到当前状态后,递推并选取出当前状态的最优解,具有无后效性和最优子结构的基本特征,其中所谓的无后效性是指:“下一时刻的状态只与当前状态有关,而和当前状态之前的状态无...
  • zhang360896270
  • zhang360896270
  • 2013年04月26日 12:14
  • 5524

递归+分治+贪心+动态规划

递归 1. 定义:一个函数在结束之前,直接或间接调用自身称为递归。 2. 思想:将一个不好解决的大问题转化为若干小问题,再把这些小问题进一步分解为更小的小问题,直至每个小问题可以直接解决为止。 3. ...
  • htyurencaotang
  • htyurencaotang
  • 2013年07月06日 17:39
  • 3061

贪心算法(二)——一般背包问题

题目 有一个背包,最多放M kg的物体(物体大小不限); 有n个物体,每个物体的重量为Wi,每个物体完全放入背包后可获得收益Pi。问:如何放置能获得最大的收益? 注:背包问题分为两种,若每个...
  • u010425776
  • u010425776
  • 2017年04月05日 21:19
  • 1739

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

描述 Petra and Jan have just received a box full of free goodies, and want to divide the goodies ...
  • libin56842
  • libin56842
  • 2014年03月29日 23:43
  • 1216

uva 12260 Free Goodies DP+一点点贪心

Free Goodies Petra and Jan have just received a box full of free goodies, and want todivide the goo...
  • hitwhacmer1
  • hitwhacmer1
  • 2014年08月10日 21:27
  • 463

LA 4945 Free Goodies(贪心)

题意:n个糖果,两个人 Petra,Jan,每个糖果对于这两个人都有一定的val值,Petra每次选对于自己val值最大的那个,如果一样,就选Jan的val值小的,Jan想让自己选的糖果的val值之和...
  • u014028317
  • u014028317
  • 2015年05月12日 01:10
  • 590

UVA 12260 Free Goodies (DP)

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

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

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

简单的DP运用+贪心算法

  • 2006年02月23日 09:05
  • 484B
  • 下载

SDUT 3923 打字 (贪心)

Problem Description snow 是个热爱打字的家伙,每次敲出更快的速度都会让他很开心。现在,他拿到一篇新的打字文章,已知这篇文章只有 26 个小写英文字母,给出 snow 打出这...
  • qq_28954601
  • qq_28954601
  • 2017年06月05日 12:52
  • 272
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:SDUT 3222 Free Goodies(贪心+dp)
举报原因:
原因补充:

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