SHARE
TWEET

Untitled

a guest Feb 18th, 2020 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <sstream>
  5. #include <fstream>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. vector<int> pizzas;
  11.  
  12. vector<bool> used;
  13.  
  14. int main() {
  15.  
  16.     string fileName;
  17.     cin >> fileName;
  18.  
  19.     ifstream file("C:\\Projects\\Hashcode2020\\" + fileName + ".in");
  20.     ofstream result("C:\\Projects\\Hashcode2020\\" + fileName + ".txt");
  21.  
  22.     int maxPizzaSize, typesOfPizza;
  23.     file >> maxPizzaSize >> typesOfPizza;
  24.  
  25.     pizzas.resize(typesOfPizza);
  26.     used.resize(typesOfPizza);
  27.  
  28.     for (int i = 0; i < typesOfPizza; i++) {
  29.         file >> pizzas[i];
  30.     }
  31.  
  32.     sort(pizzas.begin(), pizzas.end());
  33.     vector<int>::iterator end = pizzas.end();
  34.     while (true) {
  35.  
  36.         vector<int>::iterator low = lower_bound(pizzas.begin(), end, maxPizzaSize);
  37.         int index = (low - pizzas.begin()) - 1;
  38.         if (index < 0) {
  39.             break;
  40.         }
  41.         maxPizzaSize -= pizzas[index];
  42.         if (maxPizzaSize < 0) {
  43.             break;
  44.         }
  45.         end = low - 1;
  46.         used[index] = true;
  47.     }
  48.  
  49.     stringstream s;
  50.     int counter = 0;
  51.  
  52.     for (int i = 0; i < used.size(); i++) {
  53.         if (used[i]) {
  54.             counter++;
  55.             s << i << " ";
  56.         }
  57.     }
  58.     result << counter << endl;
  59.     result << s.str() << endl;
  60.  
  61.     cout << "Successfully created file " + fileName << ".txt" << endl;
  62.     cin >> maxPizzaSize;
  63.     return 0;
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top