-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitwise_transactions.cpp
101 lines (88 loc) · 2.3 KB
/
splitwise_transactions.cpp
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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <stack>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <sstream>
#include <iomanip>
using namespace std;
// class person_compare
// {
// public:
// bool operator()(pair<string, int> p1, pair<string, int> p2)
// {
// return p1.second < p2.second;
// }
// };
int32_t main()
{
int no_of_transactions, friends;
cin >> no_of_transactions >> friends;
string x, y;
int amount;
unordered_map<string, int> net;
while (no_of_transactions)
{
cin >> x >> y >> amount;
if (!net.count(x))
net[x] = 0;
if (!net.count(y))
net[y] = 0;
net[x] -= amount;
net[y] += amount;
no_of_transactions--;
}
//Using multiset so that we can have multiple equal values in a sorted order
multiset<pair<int, string> > m;
for (auto p : net)
{
string person = p.first;
int amount = p.second;
if (net[person] != 0)
m.insert(make_pair(amount, person));
}
int count = 0;
while (!m.empty())
{
auto low = m.begin();
auto high = prev(m.end());
//pop out two elements from start and end as they would be the max credit and max credit
int debit = low->first;
string debit_person = low->second;
int credit = high->first;
string credit_person = high->second;
m.erase(low);
m.erase(high);
int settled_amount = min(-debit, credit);
debit += settled_amount;
credit -= settled_amount;
cout << debit_person << " will pay " << settled_amount << " to " << credit_person << endl;
//The remainder of the settlement is pushed back into the multiset
if (debit != 0)
{
m.insert(make_pair(debit, debit_person));
}
if (credit != 0)
{
m.insert(make_pair(credit, credit_person));
}
count++;
}
cout << "Total number of transactions to be done -> " << count << endl;
}