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. winform 通过驱动注册ID检测是否已安装驱动

    public bool IsRegistered() { string clsid = ConfigurationManager.AppSettings["clsid"]; //参 ...

  2. 解析:type t_string is table of varchar2(32767) index by binary_integer

    @ 理解一: table 相当于是数组,这里定义了一个数组类型t_string; INDEX BY BINARY_INTEGER这里是定义数组下标是整数,因为ORACLE中下标可以是字符串. VARC ...

  3. MySQL(四)之MySQL数据类型

    一.数据类型概述 MySQL的数据类型有大概可以分为5种,分别是 整数类型.浮点数类型和定点数类型.日期和时间类型.字符串类型.二进制类型.现在可以来看看你对这5种类型的熟悉程度,哪个看起来懵逼了,那 ...

  4. spring整合mybatis错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist

    spring 整合Mybatis 运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:class path reso ...

  5. Redis在java中的使用

    1.首先安装Redis环境.可以在Windows.linux.别的服务器上搭建Redis环境 2.在java工程中导入必须的jar包 如maven上导入两个jar] <!-- Redis必须包 ...

  6. tomcat 和 jboss access log 日志输出详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt179 工作中nginx+jboss/tomcat反向代理集成,想打开后端jb ...

  7. el表达式里面fn的用法

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp31 头部加入标签库 <%@ taglib prefix=" ...

  8. 寻找Harris、Shi-Tomasi和亚像素角点

    Harris.Shi-Tomasi和亚像素角点都是角点,隶属于特征点这个大类(特征点可以分为边缘.角点.斑点). 一.Harris角点检测是一种直接基于灰度图像的角点提取算法,稳定性较高,但是也可能出 ...

  9. 自制裸眼3D图【推荐】

    Welcome to the World of Hidden 3D Stereograms.欢迎进入隐身3D图的世界! 网址:http://hidden-3d.com 裸眼立体图是什么? 立体图是立体 ...

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

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1. 常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自 ...