119.cpp

Back Download
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
// 119.cpp http://acm.uva.es/p/v1/119.html
//
// Solution to ACM problem #119 - "Greedy Gift Givers"
//
// Matthew Eagar - meagar@gmail.com
// Verified to work as of Feb 19 / 2005
//
// Updates for standards-compliance and best-practices on August 20 / 2007
// Verified to work as of August 20 / 2007
#include <string>
#include <iostream>
#include <map>
#include <vector>
class Person {
public:
Person() : name(), numGiven(0), numRecieved(0) { }
std::string name;
int numGiven;
int numRecieved;
inline void give(int amount) { numGiven = amount; }
inline void receive(int amount) { numRecieved += amount; }
inline int assets() { return numRecieved - numGiven; }
};
int main(int argc, char** argv) {
typedef std::vector<Person> Index;
typedef std::map<std::string, Person*> Map;
bool firstOutput = true;
std::size_t n = 0;
while (std::cin >> n) {
// avoid printing leading whitespace
if (!firstOutput) std::cout << std::endl;
else firstOutput = false;
Map peopleMap;
Index people(n);
// read names
for (Index::iterator it = people.begin(); it != people.end(); ++it) {
std::cin >> it->name;
peopleMap[it->name] = &(*it);
}
std::string giverName, recieverName;
std::size_t numRecievers = 0, numGiven = 0;
// read in people each person gives to
for (std::size_t i = 0; i < n; ++i) {
std::cin >> giverName >> numGiven >> numRecievers;
Person* giver = peopleMap[giverName];
for (std::size_t j = 0; j != numRecievers; ++j) {
std::cin >> recieverName;
peopleMap[recieverName]->receive(numGiven / numRecievers);
giver->give(numGiven - (numGiven % numRecievers));
}
}
for (Index::iterator it = people.begin(); it != people.end(); ++it) {
std::cout << it->name << " " << it->assets() << std::endl;
}
}
}