-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11_milk.cpp
101 lines (77 loc) · 1.98 KB
/
11_milk.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
98
99
100
101
/*
ID: 27ckuzm1
TASK: milk
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;
class Farm {
public:
int price;
int amount;
int totalCost() {
return price * amount;
}
};
Farm splitTimes(string data) {
istringstream iss(data);
Farm newFarm;
iss >> newFarm.price >> newFarm.amount;
iss.clear();
return newFarm;
}
int main() {
// get data from document
ifstream input;
input.open("milk.in");
string data;
getline(input, data);
stringstream ss(data);
int n, m;
ss >> n >> m;
cout << n << "\n" << m << endl;
vector<Farm> farms;
for ( int i = 0; i < m; ++i ) {
string rawString;
getline( input, rawString );
farms.push_back(splitTimes(rawString));
rawString.clear();
rawString.shrink_to_fit();
}
input.close();
// get total amount
int total = 0;
int milkGathered = 0;
while (milkGathered < n) {
int cheapestIndex = 0;
for (int i=0; i < farms.size(); ++i) {
if (farms[cheapestIndex].price >= farms[i].price) {
cout << farms[cheapestIndex].price << ' ' << farms[i].price << endl;
cheapestIndex = i;
}
}
if (milkGathered + farms[cheapestIndex].amount > n) {
farms[cheapestIndex].amount = n - milkGathered;
milkGathered = n;
total += farms[cheapestIndex].price * farms[cheapestIndex].amount;
} else {
milkGathered += farms[cheapestIndex].amount;
total += farms[cheapestIndex].price * farms[cheapestIndex].amount;
}
farms.erase(farms.begin() + cheapestIndex);
}
farms.clear();
farms.shrink_to_fit();
// output total
ofstream output;
output.open("milk.out");
output << total << endl;
output.close();
return 0;
}