-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10137.cpp
73 lines (65 loc) · 1.53 KB
/
P10137.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
#include <iostream>
#include <stdio.h>
int readInt(const std::string line) {
int ret = 0;
for(unsigned int i = 0; true; ++i) {
char c = line[i];
if(c == '.' || c == ',') {
continue;
}
if(c >= '0' && c <= '9') {
ret = 10*ret + (c-'0');
continue;
}
//std::cerr << "Read " << ret << std::endl;
return ret;
}
}
int computeFor(int low, int high, int N, int *paid) {
int paidTotal = 0;
int bank = 0;
for(int i = 0; i < N; ++i) {
if(paid[i] <= low) {
int pay = low - paid[i];
paidTotal += pay;
bank += pay;
}
else {
int receive = paid[i] - high;
bank -= receive;
}
}
if(bank < 0)
paidTotal -= bank; // More needs to be paid!
return paidTotal;
}
/*
Make paying for the trip fair for all students.
*/
int main() {
while(true) {
// Parse input:
std::string line;
std::getline(std::cin, line);
int N = readInt(line);
if(N == 0)
return 0;
int paid[10000];
int sum = 0;
for(int i = 0; i < N; ++i) {
std::getline(std::cin, line);
int val = readInt(line);
paid[i] = val;
sum += val;
}
// Compute output:
int avg = (sum+N/2)/N;
//std::cerr << " AVERAGE: " << avg << std::endl;
int pay1 = computeFor(avg-1, avg, N, paid);
int pay2 = computeFor(avg, avg+1, N, paid);
int pay = pay1 < pay2 ? pay1 : pay2;
//std::cerr << " TOTAL TRANSACTION: " << pay << std::endl;
printf("$%i.%02i\n", pay/100, pay%100);
//std::cerr << " " << pay << std::endl;
}
}