Problem can be found here here
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
| /*
ID: chinux1
PROG: gift1
LANG: C++11
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
using namespace std;
class person
{
private:
string _name;
long _amount, _initialAmount;
public:
person();
person(string name, long money);
void give(person & receiver);
void send(vector<string>& list);
void setInitialAmount(const long & money);
vector<string> giftees;
long netAmount();
};
static map<string, person>nameLookupTable;
static vector<string> orderOfName;
person::person()
{
_name = "no name";
_amount = 0;
_initialAmount = 0;
giftees = vector<string>{};
}
person::person(string name, long money)
{
_name = name;
_amount = 0;
_initialAmount = money;
giftees = vector<string>{};
}
void person::give(person &receiver)
{
if (giftees.size() == 0) {
return;
}
receiver._amount += _initialAmount / giftees.size();
}
void person::send(vector<string> &list)
{
if (list.size() == 0) {
_amount = _amount + _initialAmount;
} else {
for (auto person : list) {
give(nameLookupTable[person]);
}
_amount = _amount + _initialAmount % list.size();
}
}
void person::setInitialAmount(const long &money)
{
_initialAmount = money;
}
long person::netAmount()
{
return _amount - _initialAmount;
}
int main() {
ofstream fout ("gift1.out");
ifstream fin ("gift1.in");
// Line 1
int sizeOfGroup = 0;
fin >> sizeOfGroup;
// Line 2..NP+1
for (int i = 0; i < sizeOfGroup; i++) {
string name;
fin >> name;
person aPerson = person(name, 0);
nameLookupTable.insert(nameLookupTable.end() ,std::pair<string, person>{name, aPerson});
orderOfName.insert(orderOfName.end(), name);
}
// Line NP+2..end
while (!fin.eof()) {
string name;
fin >> name;
int initialAmount, numOfGiftees;
fin >> initialAmount >> numOfGiftees;
nameLookupTable[name].setInitialAmount(initialAmount);
for (int i = 0; i < numOfGiftees; i++) {
string nameOfGiftee;
fin >> nameOfGiftee;
nameLookupTable[name].giftees.push_back(nameOfGiftee);
}
nameLookupTable[name].send(nameLookupTable[name].giftees);
}
for (string name : orderOfName) {
fout << name << " " << nameLookupTable[name].netAmount() << endl;
}
return 0;
}
|