LA 4945 Free Goodies(贪心)
来源:互联网 发布:中国城市化进程数据 编辑:程序博客网 时间:2024/06/02 03:41
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
InputOutput34Petra100 8070 8050 8030 504Petra10 11 106 64 47Jan4 13 12 11 11 21 31 4
170 13014 169 10
题意:n个糖果,两个人 Petra,Jan,每个糖果对于这两个人都有一定的val值,Petra每次选对于自己val值最大的那个,如果一样,就选Jan的val值小的,Jan想让自己选的糖果的val值之和最大,如果有多种路径,那么就选使Petra最后的val最大的那种(真是好基友,好丽友。。 = =),告诉你先手,轮着来。问你最后他们两个的分别得val值之和。
思路:贪心。先按照Petra的顺序两人逐个选取,就会得到两组,一组是Petra的,一组是Jan的,然后再倒着来,对于每个Jan选取的糖果,对于当前点再往下找,去和本来Petra的糖果进行贪心,再交换,因为Jan可以随意取,他总可以拿到这些糖果。
#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <string>#include <algorithm>#include <queue>#include <stack>using namespace std;const int MAXN = 1010;const double PI = acos(-1.0);const double e = 2.718281828459;const double eps = 1e-8;struct node{ int a,b;} p[MAXN];int vis[MAXN];int n;char name[10];bool cmp(node x,node y){ if(x.a==y.a) { return x.b<y.b; } return x.a>y.a;}int main(){ //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int Case, x, y; cin>>Case; while(Case--) { cin>>n>>name; for(int i = 0; i < n; i++) { scanf("%d %d", &p[i].a, &p[i].b); } sort(p, p+n ,cmp); for(int i = 0; i < n; i++) { if(name[0] == 'J') { vis[i] = (i%2==0)?1:0; } else { vis[i] = (i%2==0)?0:1; } } for(int i = n-1; i >= 0; i--) { int temp; if(vis[i]) { temp = i; vis[i] = 0; for(int j = i+1; j < n; j++) { if(!vis[j] && p[j].b>=p[temp].b) { temp = j; } } vis[temp] = 1; } } x = y = 0; for(int i = 0; i < n; i++) { //printf("%d ", vis[i]); if(vis[i]) { y += p[i].b; } else { x += p[i].a; } } printf("%d %d\n", x, y); } return 0;}/*LA 4945 Free Goodies(贪心).cpp*/
0 0
- LA 4945 Free Goodies(贪心)
- la 4945 free goodies,不会!
- uva12260 - Free Goodies 贪心+dp
- UVALive 4945 Free Goodies
- UVALive 4945 Free Goodies
- UVA 12260 - Free Goodies(dp+贪心)
- uva 12260 - Free Goodies(dp+贪心)
- uva 12260 Free Goodies DP+一点点贪心
- SDUT 3222 Free Goodies(贪心+dp)
- uva 12260 - Free Goodies (dp,贪心 | 好题)
- TZU2014年省赛个人热身赛1 3387:Free Goodies(DP+贪心)
- UVA - 12260 Free Goodies
- UVA-12260-Free Goodies
- Free Goodies - UVa 12260 dp
- UVA 12260 Free Goodies (DP)
- LA 3708(贪心)
- LA 2995(贪心+推理)
- LA 6530 Football 贪心
- erlang虚拟机代码执行原理
- php单一入口模式详细讲解
- C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮助。 Const 是C++中常用的类型修饰符,常类型是指使用类
- c++一些转换操作
- Soap 协议 详解
- LA 4945 Free Goodies(贪心)
- 我写的博客呢???
- 华为校园实习生招聘总结与感悟
- my experience on evernote<0>
- SVN常用命令
- 自己编写 php 框架的核心
- uva11645 Bits(数位统计)
- Linux下安装、配置PHP环境
- Jsonp常见安全漏洞分析