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个人夜里过河,总共只有一盏灯,每次最多过两个人,然后需要有人将灯送回 ...
随机推荐
- JS中的数据类型小结
首先说说JS数据类型的分类.分为标准型和typeof类型(即控制台打印,浏览器区分) 标准型:基本类型中有:number.string.boolean.undefined.null 复合类型:obj ...
- sublime中css输入分号后自动提示的烦恼
sublime开发前端确实好用,有好多些个的插件,轻量便捷,但是在使用sublime中的一些插件的时候总是会遇到困扰,跟自己想象中的不一样,比如在使用SublimeCodeIntel插件的时候,就会遇 ...
- 使用CefSharp 在C#用户控件中嵌入Chrome浏览器使用方法
CEF(Chromium Embedded Framework, 嵌入式Chromium框架)是C/C++开发的库 目前 Google Chrome(Google浏览器),Chromium浏览器,Op ...
- 如何修改int的打印内容——史上最难的JAVA面试题
序 今天看到了一个比较特别的面试题,考察的是如何改变int的System.out.print的结果.题目如下: 下面的一句话"这是初级java实习生面试题"非常挑衅的激起了大家做题 ...
- 【找不到与请求 URI匹配的 HTTP 资源】(转)
在.net下,创建一个HTTP服务,有很多方案,比较老ashx,一般处理程序(HttpHandler), Web Service SOAP协议的,数据格式是XML,HTTP协议 WCF ...
- hdu2546 01背包 重学背包
题意:给出菜的价钱和自己的余额.使自己余额最少,注意余额大于5的情况可以买任意的菜. 思路:小于5的余额不能买菜,直接输出,大于五的余额,留下5元买最贵的菜,剩下的余额进行01背包,将剩下的余额减去0 ...
- iBatis的一个问题
写了一段查询语句,条件中有一条alarmtype<>'1004'这样的条件,原来是这样写的 <![CATA[ and alarmtype<>'1004']]> 然后 ...
- 遇到的面试题-sql
sql面试题(学生表_课程表_成绩表_教师表) 原帖链接:http://bbs.csdn.net/topics/280002741 表架构 Student(S#,Sname,Sage,Ssex) 学生 ...
- HTML5新增web存储方式
客户端存储数据的两个对象为: 两个对象在使用方式没有任何区别,唯一的不同点蚀储存数据 的有效时间 ①localStorage - 没有时间限制的数据存储 除非手动删除,否则数据将一直保存在本地文件: ...
- 201521123076 《Java程序设计》 第十四周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自 ...