-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathequal.cpp
98 lines (80 loc) · 2.73 KB
/
equal.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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/*
Solution to https://www.hackerrank.com/challenges/equal/problem
Compiled as c++14
Was able to solve it only after the hints I get from people in the discussion:
I think we don't need to use dynamic programming here. First of all, if you give chocolate bars to all but chosen one, it is equivalent to take away the chocolate bar(s) from each chosen one until every one is equal. So the challenge becomes decrease from each individual 1, 2 or 5 until all are equal. Second to calculate the ops we need to use to decrease an integer n until it reaches 0 (call it the function f) is equivalent to convert 1, 5 (no need to use dynamic programming here). Finally, to solve this challenge, we find the min (call it m) of the given list, then for i from 0 to 4, we find min of ops[i]=sum(f(c-min+i)) where c is each individual colleague and thus no need to use dynamic programming here :)
baseline is not necessarily 0. It is a number between the minimum in the sequence M and the max(0, (M - 4)).
*/
/*
ops[i] = the number of operations it takes to change all collouges to m-i where m is the cluoge with the minimal initial number of chocolates
*/
std::vector<int> ops {0,0,0,0};
//assume c >= base
static int coll_to_base(int c, int base){
int op = 0;
if(c-base>5){
int n = (c-base)/5;
op +=n;
c = c - (5*n);
}
switch(c-base){
case 0:
return op;
case 1:
case 2:
case 5:
return op+1;
case 4:
case 3:
return op+2;
}
return -1;
}
static int min(vector<int> v){
int m = v[0];
for(int i = 0;i<v.size();i++){
if(v[i]<m){
m = v[i];
}
}
return m;
}
int min_ops(vector<int> coll){
int m = min(coll);
ops[0] = ops[1] = ops[2] = ops[3] = 0;
for(int i=0;i<4;i++){
//cout<<coll.size()<<"\n";
for(int c_idx=0;c_idx<coll.size();c_idx++){
int t = coll_to_base(coll[c_idx],m-i);
//if(t == -1)
// std::cout <<i<<" "<<c_idx<<" "<<t<<"bug\n";//TODO -remove!!
ops[i]+=t;
}
}
return min(ops);
}
int main() {
int t;
cin >> t;
//std::cout<<t<<"\n";
for(int i = 0;i<t;i++){
int ncoll;
cin >> ncoll;
vector<int> coll(ncoll);
//std::cout<<ncoll<<" "<<i<<" "<<coll.size()<<"\n";
for(int j = 0;j<ncoll;j++){
int c;
cin >> c;
coll[j] = c;
}
std::cout<<min_ops(coll)<<"\n";
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}