Free Goodies UVA - 12260 贪心
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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 |
170 130 |
#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 贪心的更多相关文章
- Free Goodies UVA - 12260
Petra and Jan have just received a box full of free goodies, and want to divide the goodies between ...
- 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定 ...
- UVA 11389(贪心问题)
UVA 11389 Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description II ...
- uva 10154 贪心+dp
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVa 11389 (贪心) The Bus Driver Problem
题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线. 给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r. 问如何分配路线才能使加班费最少. 分析: 感 ...
- UVa 1467 (贪心+暴力) Installations
题意: 一共有n项服务,每项服务有安装的时间s和截止时间d.对于每项任务,如果有一项超出截止时间,惩罚值为所超出时间的长度.问如何安装才能使惩罚值最大的两个任务的惩罚值之和最小. 分析: 如果是求总惩 ...
- Party Games UVA - 1610 贪心
题目:题目链接 思路:排序后处理到第一个不同的字符,贪心一下就可以了 AC代码: #include <iostream> #include <cstdio> #include ...
- UVa 1149 (贪心) Bin Packing
首先对物品按重量从小到大排序排序. 因为每个背包最多装两个物品,所以直觉上是最轻的和最重的放一起最节省空间. 考虑最轻的物品i和最重的物品j,如果ij可以放在一个包里那就放在一起. 否则的话,j只能自 ...
- UVA 10037 贪心算法
题目链接:http://acm.hust.edu.cn/vjudge/contest/122829#problem/A 题目大意:N个人夜里过河,总共只有一盏灯,每次最多过两个人,然后需要有人将灯送回 ...
随机推荐
- Linux虚拟机之间实现密钥登陆
Server1与Server2在同一虚拟网络当中,在Server2中使用Server1的Hostname连接Server1,并且无需密码认证. Server1, Hostname: hlmvmea ...
- MySql-python的API手记
--------------------python控制mysql的API--------------------#import MySQLdb:引用对应的开发包#conn=MySQLdb.conne ...
- 修改 Pattern代码使 Java 正则表达式支持下划线 '_'
为什么 由于工作是做数据ETL的,很多时候会使用到正则对数据进行提取,但是java的正则中的groupname不支持'_',官方的文档中是这样的: Group name A capturing gro ...
- Python 多线程库总结
多线程库总结 基于线程的并行性 threading模块 下面是一些基础函数,函数包括: 函数 threading.active_count() threading.current_thread() t ...
- Java线程调度—休眠
线程休眠的方法是Thread.sleep(long millis) 和Thread.sleep(long millis, int nanos) ,均为静态方法,那调用sleep休眠的哪个线程呢?简单说 ...
- NHibernate教程(5)--CRUD操作
NHibernate之旅(5):探索Insert, Update, Delete操作 2008-10-17 16:31 by 李永京, 42903 阅读, 73 评论, 收藏, 编辑 本节内容 操作 ...
- 【转】为什么选择Spring Boot作为微服务的入门级微框架
本文为普元云计算高级工程师许二虎在普元云计算架构设计群的微课堂分享.如需加入普元新一代数字化企业云平台研发设计群参与微课堂.架构设计与讨论直播,请直接回复此公众号:"加群 姓名 公司 职位 ...
- Java GUI+mysql+分页查询
1.要求 : 创建一个学生信息管理数据库 2.实现分页查询 代码如下: a)学生实体类: /** * @author: Annie * @date:2016年6月23日 * @description: ...
- 微信小程序view标签以及display:flex的测试
一:testview.wxml,testview.js自动生成示例代码 //testview.wxml <view class="section"> <view ...
- 201521123065《java程序设计》第9周学习总结
1. 本周学习总结 2. 书面作业 本次PTA作业题集异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避免? 出现的异 ...