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

[Submit]   [Go Back]   [Status]

Description

 

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

Input Output
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 <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef struct point
{
int p,j,i;
} ponit;
ponit a[];
int b[];
bool cmp(ponit x,ponit y)
{
if(x.p==y.p)return x.j<y.j;
return x.p>y.p;
}
bool cnp(ponit x,ponit y)
{
if(x.j==y.j)return x.p<y.p;
return x.j>y.j;
}
bool check()
{
int i,sum=;;
for(i=;i<;i++)
{
sum+=b[i];
if(b[i])
{
if(sum>(i+)/)return ;
}
}
return ;
}
int main()
{
int t,n,p,j,i,sum[],st;
char z[];
scanf("%d",&t);
while(t--)
{
memset(b,,sizeof(b));
scanf("%d",&n);
scanf("%s",z);
for(i=; i<n; i++)
scanf("%d%d",&a[i].p,&a[i].j);
sort(a,a+n,cmp);
sum[]=sum[]=;
if(strcmp(z,"Petra")==)sum[]+=a[].p,st=;
else st=;
for(i=; st<n; i++,st++)
{
a[i].p=a[st].p;
a[i].j=a[st].j;
a[i].i=i+;
}
n=i;
sort(a,a+n,cnp);
for(i=;i<n;i++)
{
b[a[i].i]=;
if(check())sum[]+=a[i].j;
else b[a[i].i]=,sum[]+=a[i].p;
}
printf("%d %d\n",sum[],sum[]);
}
}

Free Goodies UVA - 12260 贪心的更多相关文章

  1. Free Goodies UVA - 12260

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

  2. 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)

    问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定 ...

  3. UVA 11389(贪心问题)

    UVA 11389 Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description II  ...

  4. uva 10154 贪心+dp

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  5. UVa 11389 (贪心) The Bus Driver Problem

    题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线. 给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r. 问如何分配路线才能使加班费最少. 分析: 感 ...

  6. UVa 1467 (贪心+暴力) Installations

    题意: 一共有n项服务,每项服务有安装的时间s和截止时间d.对于每项任务,如果有一项超出截止时间,惩罚值为所超出时间的长度.问如何安装才能使惩罚值最大的两个任务的惩罚值之和最小. 分析: 如果是求总惩 ...

  7. Party Games UVA - 1610 贪心

    题目:题目链接 思路:排序后处理到第一个不同的字符,贪心一下就可以了 AC代码: #include <iostream> #include <cstdio> #include ...

  8. UVa 1149 (贪心) Bin Packing

    首先对物品按重量从小到大排序排序. 因为每个背包最多装两个物品,所以直觉上是最轻的和最重的放一起最节省空间. 考虑最轻的物品i和最重的物品j,如果ij可以放在一个包里那就放在一起. 否则的话,j只能自 ...

  9. UVA 10037 贪心算法

    题目链接:http://acm.hust.edu.cn/vjudge/contest/122829#problem/A 题目大意:N个人夜里过河,总共只有一盏灯,每次最多过两个人,然后需要有人将灯送回 ...

随机推荐

  1. 自己动手写java 字节流输入输出流

    数据流是一串连续不断的数据的集合,就象水管里的水流,在水管的一端一点一点地供水,而在水管的另一端看到的是一股连续不断的水流.   "流是磁盘或其它外围设备中存储的数据的源点或终点." ...

  2. Javascript用数组实现栈和队列

    栈是遵循后进先出(LIFO)规则的一种有序集合,比如桌上的一叠书,我们只能从上面放或取. 队列是遵循先进先出(FIFO)规则的一种有序集合,比如排队,先排到的先离开. 数组也是一种有序的集合,它与上面 ...

  3. 201521123084 《Java程序设计》第10周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 1)线程:指进程中的一个执行流程: 2)一个进程可以同时运行多个不同线程,不同线程执行不同的任务: 3)创建 ...

  4. 团队作业8——第二次项目冲刺(Beta阶段)--第六天

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 学号 成员 贡献比 201421123001 廖婷婷 16% 201421123002 翁珊 16% 201421123004 ...

  5. 201521123077 《Java程序设计》第7周学习总结

    1. 本周学习总结 (图片来自网络) 可以看到,java的容器很多,这里讲一下这周经常用到的 ArrayList:用数组形式保存数据的容器,随机访问比较快,但是插入删除操作都比较耗时,会自动调整内部数 ...

  6. Java第七周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料: XMind 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 ...

  7. 201521123108 《Java程序设计》第4周学习总结

    1. 本章学习总结 2. 书面作业 Q1. 注释的应用 使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图) 答: Q2. 面向对象设计(大作业1-非常重要) 2. ...

  8. 201521123013 《Java程序设计》第14周学习总结

    1. 本周学习总结 2. 书面作业 Q1. MySQL数据库基本操作 1.1 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自己的学号.姓名) 1.2 在自己建立的数据库上执行常见SQ ...

  9. 201521123056 《Java程序设计》第9周学习总结

    1. 本周学习总结 1.1 思维导图如下: 2. 书面作业 本次PTA作业题集异常 1. 常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要 ...

  10. 201521123024 《Java程序设计》第13周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容.