-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalanced_Rating_Changes.cpp
More file actions
74 lines (67 loc) · 1.84 KB
/
Balanced_Rating_Changes.cpp
File metadata and controls
74 lines (67 loc) · 1.84 KB
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
// n participants and ith participant is expecting ai change in rating
// balanced rating change means sum of all rating changes are 0
// now we need new ratings which is rounded of to nearest integer when divided by 2
// it should also be balanced
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int n;
cin >> n;
vector<ll> a(n);
vector<int> b(n);
vector<int> to_floor;
vector<int> to_ceil;
ll sum = 0;
// Compute the initial rounded ratings and their sum
for (int i = 0; i < n; i++)
{
cin >> a[i];
float temp = a[i] / 2.0;
int rounded = round(temp);
b[i] = rounded;
sum += rounded;
// Track indices where we can adjust the value
if (temp - floor(temp) == 0.5)
{
if (rounded == floor(temp))
to_ceil.push_back(i);
else
to_floor.push_back(i);
}
}
// Adjust the ratings if the sum is not zero
if (sum != 0)
{
// Calculate the amount of adjustment needed
int adjustments_needed = abs(sum);
if (sum > 0)
{
// Decrease some values from ceil to floor
for (int i = 0; i < adjustments_needed && !to_floor.empty(); i++)
{
int idx = to_floor.back();
to_floor.pop_back();
b[idx]--;
sum--;
}
}
else
{
// Increase some values from floor to ceil
for (int i = 0; i < adjustments_needed && !to_ceil.empty(); i++)
{
int idx = to_ceil.back();
to_ceil.pop_back();
b[idx]++;
sum++;
}
}
}
// Print the final ratings
for (int integer : b)
{
cout << integer << endl;
}
}