https://train.usaco.org/usacoprob2?a=73eEaWy965w&S=gift1

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

题目描述

对于一群 nn 个要互送礼物的朋友,GY 要确定每个人送出的钱比收到的多多少。在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人。

然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较多的钱。

给出一群朋友,没有人的名字会长于 1414 字符,给出每个人将花在送礼上的钱,和将收到他的礼物的人的列表,请确定每个人收到的比送出的钱多的数目。

输入格式

第一行一个正整数 nn,表示人数。 接下来nn 行,每行一个字符串表示人名。

接下来有 LL 段内容,对于每一段: 第一行是将会送出礼物人的名字。
第二行包含二个非负整数,第一个是原有的钱的数目 ( \in [0,2000]∈[0,2000] ),第二个 g_igi​ 是将收到这个人礼物的人的个数 如果 g_i \neq 0gi​​=0, 在下面 g_igi​ 行列出礼物的接受者的名字,一个名字一行。

输出格式

输出共 nn 行,每行输出一个人的名字和该人收到的钱比送出的钱多的数目。名字的顺序应该与输入第 22 行至 n+1n+1 行的顺序相同。

送出的钱永远是整数,即假设送礼人一次向 mm 人送出 nn 元,每个人应该得到 \lfloor n/m \rfloor⌊n/m⌋ 元。剩余未送出的钱应返还给送礼者。

输入输出样例

输入 #1复制

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

输出 #1复制

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

说明/提示

【数据范围】
1\le n \le 101≤n≤10

解析:注意循环判断后需要break

/*
ID:  L
PROG: gift1
LANG: C++
*/
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
struct s1{string name;int money;
}s[12];
int main()
{freopen("gift1.in","r",stdin);freopen("gift1.out","w",stdout);ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);int n;int k = 0;cin >> n;for(int i = 1; i <= n; ++i)//输入姓名和钱 {cin >> s[i].name;s[i].money = 0;}//   for(int i = 1; i <= n; ++i)//    cout << s[i].name << endl;for(int i = 1;  i <= n; ++i){string name2,name3;cin >> name2;//送礼者 int a,b;cin >> a >> b;//将a元钱送给b个人 if(b == 0) continue;//需要特判,不然除数为0会报错 for(int u = 1; u <= n; ++u)//送礼者钱减少 {if(name2.compare(s[u].name) == 0){s[u].money = s[u].money - a + a%b;break;//  cout << s[u].name << " " << s[u].money;}}for(int j = 1; j <= b; ++j){cin >> name3;//接受礼物者 for(int u = 1; u <= n; ++u){if(name3.compare(s[u].name) == 0)//接受礼物者评分送礼者的钱 {s[u].money += a/b;break;//   cout << s[u].name << " " << s[u].money << endl;}}}}for(int u = 1; u <= n; ++u) {cout << s[u].name << " " << s[u].money << endl;}return 0;
}

使用map

map<string,int> mp; string 为key

mp["dave"] = 302

/*
ID: L
PROG: gift1
LANG: C++
*/
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<cstdio>
using namespace std;
map<string,int> mp;
string s[12];
int s2[12];
int main()
{freopen("gift1.in","r",stdin);freopen("gift1.out","w",stdout);
//  ios::sync_with_stdio(false); // cin.tie(0);cout.tie(0);int n;int k = 0;cin >> n;for(int i = 1; i <= n; ++i)//输入姓名和钱 {cin >> s[i];s2[i] = 0;}// for(int i = 1; i <= n; ++i)//    cout << s[i].name << endl;for(int i = 1;  i <= n; ++i){string name2,name3;cin >> name2;//送礼者 int a,b;cin >> a >> b;//将a元钱送给b个人 if(b == 0) continue;//需要特判,不然除数为0会报错 mp[name2] = mp[name2] - a + a%b;for(int j = 1; j <= b; ++j){cin >> name3;//接受礼物者 mp[name3] += a/b;}}for(int i = 1; i <= n; ++i) {cout << s[i] << " " << mp[s[i]] << endl;}return 0;
}

1.1.2 USACO Task 'gift1': Greedy Gift Givers相关推荐

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

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

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

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

  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. USACO1.1.2 - Greedy Gift Givers

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

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

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

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

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

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

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

  9. USACO 1.0_Greedy Gift Givers

    2019独角兽企业重金招聘Python工程师标准>>> /* ID: zfb2 LANG: C++ TASK: gift1 */ #include <iostream> ...

  10. USACO 2017 December Contest Platinum T3: Greedy Gift Takers

    题目大意 有 N(1≤N≤1e5)头牛按顺序排成一列,编号从 1 到 N,1 号牛在队头,N 号牛在队尾. 每次位于队头的牛 i 拿到一个礼物,然后插入到从队尾数ci​头牛之前的位置..举个栗子: 初 ...

最新文章

  1. 全面对比,深度解析 Ignite 与 Spark
  2. Python 技术篇-PIL库安装及截图功能演示
  3. 系统已有MYSQL环境,如何安装宝塔面板
  4. SpringBoot—自定义线程池及并发定时任务模板
  5. js调用微信扫一扫demo_JS 调用微信扫一扫功能
  6. idea中运行maven安装jar包到本地仓库跳过test
  7. Mysql for Linux安装配置之——二进制安装
  8. Vue的使用技巧是什么,学习难度怎么样?
  9. 解决设置redmineblacklog的按钮无效问题
  10. 输入流控制:几种清除输入流中空格或回车的常用函数
  11. 关于Visual Studio “当前不会命中断点.还没有为该文档加载任何符号“的解决方法
  12. 35岁是继续做测试,还是回家送外卖?
  13. LAMMPS实例教程—In文件详解
  14. java javah_Javah 常见错误记录
  15. 如何学习plc编程?(核心秘诀分享)
  16. vue项目如何做到每30秒刷新1次接口?
  17. CentOS 6.3安装 flash控件成功案例(其它方法未成功)
  18. Centos7 安装teamviewer
  19. 计算机表格用计算公式百分百,excel表格怎么算数据的阳性比率-计算机一级用excel表格计算增长比例该怎么算?...
  20. Bluetooth Core Architecture Blocks----蓝牙核心架构

热门文章

  1. HTMLCSS+JavaScript前端面试题
  2. CSS box-flex属性,然后弹性盒子模型简介
  3. python语言特点多模型_GitHub - zkyzq/py-kenlm-model: python | 高效使用统计语言模型kenlm:新词发现、分词、智能纠错等...
  4. SuperMap iDesktopX 地图制图专栏目录(持续更新中)
  5. Shell Script基础-Bash变量和逻辑运行
  6. sql学习之单行函数
  7. 彻底清除通过Wubi方式在Windows下安装的Ubuntu Linux
  8. 交换机的原理以及华为交换机简单命令
  9. 影评 August Rush
  10. 整体绩效管理-整合企业绩效与人员绩效(zt)