-
Notifications
You must be signed in to change notification settings - Fork 0
/
GreedyFlorist.cpp
41 lines (39 loc) · 1.21 KB
/
GreedyFlorist.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
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
int i, j, h;
int n, k;
int tot, cur;
int added;
std::vector<int> arr;
std::vector<int> v;
int flower, friends;
//First Step is sorting the stuff they put in
int main(){
std::cin >> flower >> friends;
for(i = 0; i < flower; ++i){
std::cin >> n;
arr.push_back(n);
n = 0;
}
//We need to reverse the array in order to go from greatest to smallest and give each flower to each person and we need to sort it
std::sort (arr.begin(), arr.end());
for(h = flower - 1; h >= 0; --h){
v.push_back(arr[h]);
}
//Above is the decreased prices in a new array
while(cur != friends){
for(j = cur; j < flower; j += friends){
tot += (v[j] * (1 + added));
++added;
}
++cur;
added = 0;
//This resets the amount of extra flowers they've bought
//It also Adds one to the current friend that we're on, one we hit the number of friends the loop ends
//The actual loop Starts at the friend number and we add the amount of friends because it should get us back to the friend that's buying flowers, it ends once we're out flowers
}
std::cout << tot;
return 0;
}