Free Goodies UVA - 12260 贪心

本文介绍了一个关于两人如何公平分配一系列不同价值礼物的算法问题。通过制定特定策略,确保双方在选择过程中达到价值最大化且考虑对方感受。文章详细描述了输入输出格式及示例。
 
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

Download as PDF
 

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

InputOutput
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

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 #include <math.h>
 6 using namespace std;
 7 typedef struct point
 8 {
 9     int p,j,i;
10 } ponit;
11 ponit a[1005];
12 int b[1005];
13 bool cmp(ponit x,ponit y)
14 {
15     if(x.p==y.p)return x.j<y.j;
16     return x.p>y.p;
17 }
18 bool cnp(ponit x,ponit y)
19 {
20     if(x.j==y.j)return x.p<y.p;
21     return x.j>y.j;
22 }
23 bool check()
24 {
25     int i,sum=0;;
26     for(i=1;i<1005;i++)
27     {
28         sum+=b[i];
29         if(b[i])
30         {
31             if(sum>(i+1)/2)return 0;
32         }
33     }
34 return 1;
35 }
36 int main()
37 {
38     int t,n,p,j,i,sum[2],st;
39     char z[20];
40     scanf("%d",&t);
41     while(t--)
42     {
43         memset(b,0,sizeof(b));
44         scanf("%d",&n);
45         scanf("%s",z);
46         for(i=0; i<n; i++)
47             scanf("%d%d",&a[i].p,&a[i].j);
48         sort(a,a+n,cmp);
49         sum[0]=sum[1]=0;
50         if(strcmp(z,"Petra")==0)sum[0]+=a[0].p,st=1;
51         else st=0;
52         for(i=0; st<n; i++,st++)
53         {
54             a[i].p=a[st].p;
55             a[i].j=a[st].j;
56             a[i].i=i+1;
57         }
58         n=i;
59         sort(a,a+n,cnp);
60         for(i=0;i<n;i++)
61         {
62             b[a[i].i]=1;
63             if(check())sum[1]+=a[i].j;
64             else b[a[i].i]=0,sum[0]+=a[i].p;
65         }
66         printf("%d %d\n",sum[0],sum[1]);
67     }
68 }
View Code

 

转载于:https://www.cnblogs.com/ERKE/p/3670947.html

源码地址: https://pan.quark.cn/s/d1f41682e390 miyoubiAuto 米游社每日米游币自动化Python脚本(务必使用Python3) 8更新:更换cookie的获取地址 注意:禁止在B站、贴吧、或各大论坛大肆传播! 作者已退游,项目不维护了。 如果有能力的可以pr修复。 小引一波 推荐关注几个非常可爱有趣的女孩! 欢迎B站搜索: @嘉然今天吃什么 @向晚大魔王 @乃琳Queen @贝拉kira 第三方库 食用方法 下载源码 在Global.py中设置米游社Cookie 运行myb.py 本地第一次运行时会自动生产一个文件储存cookie,请勿删除 当前仅支持单个账号! 获取Cookie方法 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 按刷新页面,按下图复制 Cookie: How to get mys cookie 当触发时,可尝试按关闭,然后再次刷新页面,最后复制 Cookie。 也可以使用另一种方法: 复制代码 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 控制台粘贴代码并运行,获得类似的输出信息 部分即为所需复制的 Cookie,点击确定复制 部署方法--腾讯云函数版(推荐! ) 下载项目源码和压缩包 进入项目文件夹打开命令行执行以下命令 xxxxxxx为通过上面方式或取得米游社cookie 一定要用双引号包裹!! 例如: png 复制返回内容(包括括号) 例如: QQ截图20210505031552.png 登录腾讯云函数官网 选择函数服务-新建-自定义创建 函数名称随意-地区随意-运行环境Python3....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值