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 any or all of the other friends. Likewise, each friend might or might not receive money from any or all of the other friends. Your goal in this problem is to deduce how much more money each person gives than they receive.
The rules for gift-giving are potentially different than you might expect. Each person sets aside 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 3 among 2 friends would be 1 each for the friends with 1 left over -- that 1 left over stays in the giver's "account".
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 more (or less) each person in the group gives than they receive.
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 charcters, '\n' and '\r'. Do not let your program get trapped by this!
PROGRAM NAME: gift1
INPUT FORMAT
Line 1: | The single integer, NP | |||
Lines 2..NP+1: | Each line contains the name of a group member | |||
Lines NP+2..end: | NP groups of lines organized like this:
|
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
출처: http://train.usaco.org/
NP 명의 사람들이 있는 그룹에서 서로 돈을 주는데 첫 번째 줄에는 NP값, 2~NP+1 은 그룹의 멤버들, NP+2~ 은 각 case가 기입되어 있고 각 case의 첫 줄은 돈을 줄 사람, 두 번째 줄은 줄 돈과 줄 사람의 숫자 그리고 그 다음 줄 부터는 돈을 받을 사람의 이름이 기입되어 있습니다. 만약 200을 3명에게 나눠준다 할 때 돈은 소수점으로 줄 수 없으므로 66씩 주게 됩니다.
my solving
c++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #include <iostream> #include <string> #include <unordered_map> #include <fstream> using namespace std; int main() { ofstream fout("gift1.out"); ifstream fin("gift1.in"); int NP; unordered_map<string, int> members; fin >> NP; for (int i = 0; i < NP; i++) { string name; fin >> name; members.insert(make_pair(name, 0)); } string input; string giver = ""; int totalMoney = -1; int leftReceivers = -1; int money = 0; while (fin >> input) { if (giver == "") { giver = input; } else if (totalMoney == -1) { totalMoney = stoi(input); } else if (leftReceivers == -1) { leftReceivers = stoi(input); if (totalMoney == 0) money = 0; else money = totalMoney / leftReceivers; members[giver] -= (money * leftReceivers); if (leftReceivers == 0) { giver = ""; totalMoney = -1; leftReceivers = -1; } } else if (leftReceivers > 0) { members[input] += money; leftReceivers--; if (leftReceivers == 0) { giver = ""; totalMoney = -1; leftReceivers = -1; } } } //for each (auto var in members) //{ // fout << var.first << " " << var.second << endl; //} for (unordered_map<string, int>::iterator it = members.begin(); it != members.end(); it++) { fout << it->first << " " << it->second << endl; } return 0; } | cs |
'Algorithm, Data structure > Solved Algorithmic Problem' 카테고리의 다른 글
USACO 1.1 - Broken Necklace (0) | 2016.06.01 |
---|---|
USACO 1.1 - Friday the Thirteenth (0) | 2016.05.29 |
Project Euler #26 - Reciprocal cycles (0) | 2016.05.28 |
USACO 1.1 - Your Ride Is Here (0) | 2016.05.27 |
UVa - Ecological Bin Packing (0) | 2016.05.27 |