Free Goodies UVA - 12260 贪心

 
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

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

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 #include <math.h>
 6 using namespace std;
 7 typedef struct point
 8 {
 9     int p,j,i;
10 } ponit;
11 ponit a[1005];
12 int b[1005];
13 bool cmp(ponit x,ponit y)
14 {
15     if(x.p==y.p)return x.j<y.j;
16     return x.p>y.p;
17 }
18 bool cnp(ponit x,ponit y)
19 {
20     if(x.j==y.j)return x.p<y.p;
21     return x.j>y.j;
22 }
23 bool check()
24 {
25     int i,sum=0;;
26     for(i=1;i<1005;i++)
27     {
28         sum+=b[i];
29         if(b[i])
30         {
31             if(sum>(i+1)/2)return 0;
32         }
33     }
34 return 1;
35 }
36 int main()
37 {
38     int t,n,p,j,i,sum[2],st;
39     char z[20];
40     scanf("%d",&t);
41     while(t--)
42     {
43         memset(b,0,sizeof(b));
44         scanf("%d",&n);
45         scanf("%s",z);
46         for(i=0; i<n; i++)
47             scanf("%d%d",&a[i].p,&a[i].j);
48         sort(a,a+n,cmp);
49         sum[0]=sum[1]=0;
50         if(strcmp(z,"Petra")==0)sum[0]+=a[0].p,st=1;
51         else st=0;
52         for(i=0; st<n; i++,st++)
53         {
54             a[i].p=a[st].p;
55             a[i].j=a[st].j;
56             a[i].i=i+1;
57         }
58         n=i;
59         sort(a,a+n,cnp);
60         for(i=0;i<n;i++)
61         {
62             b[a[i].i]=1;
63             if(check())sum[1]+=a[i].j;
64             else b[a[i].i]=0,sum[0]+=a[i].p;
65         }
66         printf("%d %d\n",sum[0],sum[1]);
67     }
68 }
View Code

 

转载于:https://www.cnblogs.com/ERKE/p/3670947.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值