LA 4945 Free Goodies(贪心)

来源:互联网 发布:中国城市化进程数据 编辑:程序博客网 时间:2024/06/02 03:41

Description

Download as PDF

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

题意:n个糖果,两个人 Petra,Jan,每个糖果对于这两个人都有一定的val值,Petra每次选对于自己val值最大的那个,如果一样,就选Jan的val值小的,Jan想让自己选的糖果的val值之和最大,如果有多种路径,那么就选使Petra最后的val最大的那种(真是好基友,好丽友。。 = =),告诉你先手,轮着来。问你最后他们两个的分别得val值之和。


思路:贪心。先按照Petra的顺序两人逐个选取,就会得到两组,一组是Petra的,一组是Jan的,然后再倒着来,对于每个Jan选取的糖果,对于当前点再往下找,去和本来Petra的糖果进行贪心,再交换,因为Jan可以随意取,他总可以拿到这些糖果。



#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <string>#include <algorithm>#include <queue>#include <stack>using namespace std;const int MAXN = 1010;const double PI = acos(-1.0);const double e = 2.718281828459;const double eps = 1e-8;struct node{    int a,b;} p[MAXN];int vis[MAXN];int n;char name[10];bool cmp(node x,node y){    if(x.a==y.a)    {        return x.b<y.b;    }    return x.a>y.a;}int main(){    //freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);    int Case, x, y;    cin>>Case;    while(Case--)    {        cin>>n>>name;        for(int i = 0; i < n; i++)        {            scanf("%d %d", &p[i].a, &p[i].b);        }        sort(p, p+n ,cmp);        for(int i = 0; i < n; i++)        {            if(name[0] == 'J')            {                vis[i] = (i%2==0)?1:0;            }            else            {                vis[i] = (i%2==0)?0:1;            }        }        for(int i = n-1; i >= 0; i--)        {            int temp;            if(vis[i])            {                temp = i;                vis[i] = 0;                for(int j = i+1; j < n; j++)                {                    if(!vis[j] && p[j].b>=p[temp].b)                    {                        temp = j;                    }                }                vis[temp] = 1;            }        }        x = y = 0;        for(int i = 0; i < n; i++)        {            //printf("%d ", vis[i]);            if(vis[i])            {                y += p[i].b;            }            else            {                x += p[i].a;            }        }        printf("%d %d\n", x, y);    }    return 0;}/*LA 4945 Free Goodies(贪心).cpp*/


0 0