Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <sstream>
- #include <fstream>
- #include <string>
- using namespace std;
- vector<int> pizzas;
- vector<bool> used;
- int main() {
- string fileName;
- cin >> fileName;
- ifstream file("C:\\Projects\\Hashcode2020\\" + fileName + ".in");
- ofstream result("C:\\Projects\\Hashcode2020\\" + fileName + ".txt");
- int maxPizzaSize, typesOfPizza;
- file >> maxPizzaSize >> typesOfPizza;
- pizzas.resize(typesOfPizza);
- used.resize(typesOfPizza);
- for (int i = 0; i < typesOfPizza; i++) {
- file >> pizzas[i];
- }
- sort(pizzas.begin(), pizzas.end());
- vector<int>::iterator end = pizzas.end();
- while (true) {
- vector<int>::iterator low = lower_bound(pizzas.begin(), end, maxPizzaSize);
- int index = (low - pizzas.begin()) - 1;
- if (index < 0) {
- break;
- }
- maxPizzaSize -= pizzas[index];
- if (maxPizzaSize < 0) {
- break;
- }
- end = low - 1;
- used[index] = true;
- }
- stringstream s;
- int counter = 0;
- for (int i = 0; i < used.size(); i++) {
- if (used[i]) {
- counter++;
- s << i << " ";
- }
- }
- result << counter << endl;
- result << s.str() << endl;
- cout << "Successfully created file " + fileName << ".txt" << endl;
- cin >> maxPizzaSize;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement