A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money. Each of these friends might or might not give some money to some or all of the other friends (although some might be cheap and give to no one). Likewise, each friend might or might not receive money from any or all of the other friends. Your goal is to deduce how much more money each person receives than they give.

The rules for gift-giving are potentially different than you might expect. Each person goes to the bank (or any other source of money) to get a certain amount of money to give and divides this money evenly among all those to whom he or she is giving a gift. No fractional money is available, so dividing 7 among 2 friends would be 3 each for the friends with 1 left over – that 1 left over goes into the giver's "account". All the participants' gift accounts start at 0 and are decreased by money given and increased by money received.

In any group of friends, some people are more giving than others (or at least may have more acquaintances) and some people have more money than others.

Given:

  • a group of friends, no one of whom has a name longer than 14 characters,
  • the money each person in the group spends on gifts, and
  • a (sub)list of friends to whom each person gives gifts,

determine how much money each person ends up with.

IMPORTANT NOTE

The grader machine is a Linux machine that uses standard Unix conventions: end of line is a single character often known as '\n'. This differs from Windows, which ends lines with two characters, '\r\ and '\n'. Do not let your program get trapped by this!

PROGRAM NAME: gift1

INPUT FORMAT

Line # Contents
1 A single integer, NP
2..NP+1 Line i+1 contains the name of group member i
NP+2..end NP groups of lines organized like this:

The first line of each group tells the person's name who will be giving gifts.
The second line in the group contains two numbers:

  • The amount of money (in the range 0..2000) to be divided into gifts by the giver
  • NGi (0 ≤ NGi ≤ NP), the number of people to whom the giver will give gifts
If NGi is nonzero, each of the next NGi lines lists the name of a recipient of a gift; recipients are not repeated in a single giver's list.

SAMPLE INPUT (file gift1.in)

5
dave
laura
owen
vick
amr
dave
200 3
laura
owen
vick
owen
500 1
dave
amr
150 2
vick
owen
laura
0 2
amr
vick
vick
0 0

OUTPUT FORMAT

The output is NP lines, each with the name of a person followed by a single blank followed by the net gain or loss (final_money_value - initial_money_value) for that person. The names should be printed in the same order they appear starting on line 2 of the input.

All gifts are integers. Each person gives the same integer amount of money to each friend to whom any money is given, and gives as much as possible that meets this constraint. Any money not given is kept by the giver.

SAMPLE OUTPUT (file gift1.out)

dave 302
laura 66
owen -359
vick 141
amr -150

OUTPUT EXPLANATION

Five names: dave, laura, owen, vick, amr. Let's keep a table of the gives (money) each person 'has':

dave laura owen vick amr
0 0 0 0 0
First, 'dave' splits 200 among 'laura', 'owen', and 'vick'. That comes to 66 each, with 2 left over
-200+2 +66 +66 +66 0
-198 66 66 66 0
Second, 'owen' gives 500 to 'dave':
-198+500 66 66-500 66 0
302 66 -434 66 0
Third, 'amr' splits 150 between 'vick' and 'owen':
302 66 -434+75 66+75 -150
302 66 -359 141 -150
Fourth, 'laura' splits 0 between 'amr' and 'vick'; no changes:
302 66 -359 141 -150
Finally, 'vick' gives 0 to no one:
dave laura owen vick amr
302 66 -359 141 -150

用结构体做:

/*
ID: traysen1
TASK: gift1
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<string>
using namespace std;int N;
struct Person {string name;int money;
}person[20];int main() {freopen("gift1.in", "r", stdin);freopen("gift1.out", "w", stdout);scanf("%d", &N);for (int i = 0; i < N; i++) {cin >> person[i].name;person[i].money = 0;}for (int i = 0; i < N; i++) {string giver;int total_money, gift_number;cin >> giver;cin >> total_money >> gift_number;if (gift_number == 0) continue;int gift = total_money / gift_number, rest = total_money % gift_number;for (int j = 0; j < N; j++)if (person[j].name == giver) {person[j].money -= (total_money - rest);break;}while (gift_number--) {string recipient;cin >> recipient;for (int j = 0; j < N; j++)if (person[j].name == recipient) {person[j].money += gift;break;}}}for (int i = 0; i < N; i++)cout << person[i].name << " " << person[i].money << endl;return 0;
}

用map做:

/*
ID: traysen1
TASK: gift1
LANG: C++
*/
#include <iostream>
#include <map>
#include <string>
using namespace std;map<string, int> people;
string namelist[20];
int N;void input() {cin >> N;for (int i = 0; i < N; i++) {string name;cin >> name;people[name] = 0;namelist[i] = name;}
}void output() {for (int i = 0; i < N; i++) cout << namelist[i] << " " << people[namelist[i]] << endl;
}int main() {freopen("gift1.in", "r", stdin);freopen("gift1.out", "w", stdout);input();for (int i = 0; i < N; i++) {string giver;int total_money, gift_number;cin >> giver;cin >> total_money >> gift_number;if (gift_number == 0) continue;int gift = total_money / gift_number, rest = total_money % gift_number;people[giver] -= total_money - rest;while (gift_number--) {string recipient;cin >> recipient;people[recipient] += gift;}}output();return 0;
}

2020-12-30 USACO Greedy Gift Givers相关推荐

  1. USACO Training Section 1.1 贪婪的送礼者Greedy Gift Givers

    P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers 题目描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一 ...

  2. USACO题解——Section 1.2——Greedy Gift Givers

    题目地址:https://train.usaco.org/usacoprob2?a=BGOMbIJsisd&S=gift1. 或者我的OJ网站,http://47.110.135.197/pr ...

  3. 贪婪的送礼者Greedy Gift Givers [USACO 1.2]

    贪婪的送礼者Greedy Gift Givers [USACO 1.2]题目描述: 有一群(N个)要互送礼物的朋友,现在要确定每个人送出的钱比收到的钱多多少.每个人都准备了一些钱来送礼物,而这些钱将会 ...

  4. Greedy Gift Givers

    原题地址 Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange g ...

  5. USACO Section 1.2 Greedy Gift Givers (简单查找)

    2018-3-25 changed 题目大意就是说我们要互相 " 送钱 " ,一共NP个人,指定某一个人将sum这么多的钱分给num个人,那么这些人每个人多了sum/num,送钱的 ...

  6. USACO1.1.2 - Greedy Gift Givers

    贪婪礼品送货员 一组NP(2≤NP≤10)唯一命名的朋友决定交换礼物的钱.这些朋友中的每一个可能或可能不会给任何或所有其他朋友一些钱.同样,每个朋友可能或可能不从任何或所有其他朋友接收钱.你在这个问题 ...

  7. [USACO1.1]贪婪的送礼者Greedy Gift Givers

    题目描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人.然而,在任何一群朋友中 ...

  8. 【2020.12.30更新】信号处理常用公式(一)

    积化和差 cos⁡αcos⁡β=12[cos⁡(α+β)+cos⁡(α−β)]\cos \alpha \cos \beta = \frac{1}{2}[\cos (\alpha + \beta ) + ...

  9. YTU ---1402-Greedy Gift Givers 贪婪的送礼者

    1402: 1.1.2 Greedy Gift Givers 贪婪的送礼者 Time Limit: 1 Sec   Memory Limit: 64 MB Submit: 31   Solved: 1 ...

  10. DayDayUp:2019.12.30吴晓波2020年终秀演讲《预见2020:来海边,拾起信心》读后有感

    DayDayUp:2019.12.30吴晓波2020年终秀演讲<预见2020:来海边,拾起信心>读后有感 导读:2019年,过的好不好?有人豪情万丈,有人强颜欢笑. 互联网平台带来了方便快 ...

最新文章

  1. Servlet3——注解
  2. 华为员工哀叹:32岁大码农只能在华为等裁,出去薪资没人接得住!出路在哪儿?...
  3. JVM:查看java内存情况命令
  4. 第七周项目一-成员函数(4)
  5. 去除字符串中的小数点
  6. 宣告推出.NET Core 3.0 Preview 7
  7. 4、mybatis通过配置类Configuration 实现初始化
  8. std::future详解
  9. 三年硬件工程师薪水_谷歌员工基本薪资曝光:软件工程师最高 241 万,工程副总裁 325 万,产品经理、UX 171 万,硬件工程师 166 万...
  10. MySQL DBA面试高频三十问
  11. 原来你是这样的JAVA[01]-基础一瞥
  12. 二维傅里叶变换深度研究-图像与其频域关系
  13. 运行spark及hadoop版本不一致解决方法
  14. 设置电脑默认程序(针对免安装绿色版程序)
  15. Android应用开发之所有动画使用详解
  16. 计算机毕业设计ssm人工智能辅修专业教学管理系统9xg0x系统+程序+源码+lw+远程部署
  17. 12c 2cpu oracle se_Oracle许可政策变天!Oracle 12c标准版2限制多多
  18. iphone修改运营商名称_iPhone上的“运营商设置更新”弹出窗口是什么?
  19. 小米电视刷android系统升级,小米盒子跳升安卓8.0系统:针对电视等大屏产品进一步优化...
  20. duilib 子窗口位置_duilib入门简明教程 -- 界面布局(9)

热门文章

  1. AudioRecorder实时录制mp3格式音频
  2. C#开发微信门户及应用(26)-公众号微信素材管理
  3. 如何安装Prestashop
  4. 第51周 ARTS 2019 10 06
  5. 酒店如何更好地处理顾客投诉?酒店管理怎么做更高效?
  6. 寻找2018中国企业数字化转型优秀案例!
  7. 这1000道JAVA面试题,刷完50%妥妥的也能上岸
  8. api规范PHP,api及文档编写规范
  9. 【运维知识高级篇】超详细的Shell编程讲解5(普通数组+关联数组+抓阄项目)
  10. Cadence使用中遇到的错误及解决办法(更新)