-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCostToBuyNcandies.cpp
52 lines (50 loc) · 1.09 KB
/
CostToBuyNcandies.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
#include "headers.hpp"
int getMin(int *, int, int);
int getMax(int *, int, int);
p(int) minMaxCostToBuyNCandies(int *candies, int n, int k)
{
int minCost = getMin(candies, n, k);
int maxCost = getMax(candies, n, k);
return make_pair(minCost, maxCost);
}
int getMin(int *arr, int n, int k)
{
int cost = 0;
sort(arr, arr + n);
int i = 0, j = n - 1;
while (i <= j)
{
cost += arr[i];
j -= k;
i++;
}
return cost;
}
int getMax(int *arr, int n, int k)
{
int cost = 0;
sort(arr, arr + n, greater<int>());
int i = 0, j = n - 1;
while (i <= j)
{
cost += arr[i];
j -= k;
i++;
}
return cost;
}
int main()
{
vector<p(int)> output;
tests(t)
{
int n, k;
cin >> n >> k;
int *candies = new int[n];
loop(i, n) cin >> candies[i];
output.pb(minMaxCostToBuyNCandies(candies, n, k));
}
loop(i, output.size()) cout << "case #" << i + 1 << " : Min->" << output[i].first << " Max->"
<< output[i].second << endl;
return 0;
}