Advertisement
Sanlover

Untitled

Nov 8th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. class Heap :public vector<size_t>
  7. {
  8. public:
  9. size_t mass;
  10. Heap() : mass(0) {}
  11. };
  12.  
  13. int main()
  14. {
  15. size_t nRocks;
  16. cout << "Enter the number of rocks = ";
  17. cin >> nRocks;
  18.  
  19. vector<size_t> rocks(nRocks);
  20. Heap first, second;
  21.  
  22. for (size_t i = 0; i < nRocks; i++)
  23. {
  24. cout << "[" << i + 1 << "] ";
  25. cin >> rocks[i];
  26. }
  27.  
  28. sort(rocks.rbegin(), rocks.rend());
  29.  
  30. first.push_back(rocks.front());
  31. first.mass += first.front();
  32.  
  33. rocks.erase(rocks.begin());
  34.  
  35. for (auto& it : rocks)
  36. {
  37. if (first.mass > second.mass)
  38. {
  39. second.push_back(it);
  40. second.mass += it;
  41. }
  42. else
  43. {
  44. first.push_back(it);
  45. first.mass += it;
  46. }
  47. }
  48.  
  49. cout << endl << "Results" << endl;
  50.  
  51. if ((first.mass > second.mass ? static_cast<float>(first.mass) : static_cast<float>(second.mass)) /
  52. (first.mass > second.mass ? static_cast<float>(second.mass) : static_cast<float>(first.mass)) <= 1.5f)
  53. {
  54. cout << "First heap: " << endl;
  55. for (auto& it : first)
  56. cout << it << " ";
  57. cout << endl << "First heap mass = " << first.mass << endl;
  58.  
  59. cout << endl << "Second heap" << endl;
  60. for (auto& it : second)
  61. cout << it << " ";
  62. cout << endl << "Second heap mass = " << second.mass << endl;
  63. }
  64. else
  65. {
  66. cout << endl << "First heap mass = " << first.mass << endl;
  67. cout << endl << "Second heap mass = " << second.mass << endl;
  68. cout << endl << "Difference = " << (first.mass > second.mass ? static_cast<float>(first.mass) : static_cast<float>(second.mass)) / (first.mass > second.mass ? static_cast<float>(second.mass) : static_cast<float>(first.mass));
  69. }
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement