Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <iterator>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. void swap(long *xp, long *yp)
  8. {
  9. int temp = *xp;
  10. *xp = *yp;
  11. *yp = temp;
  12. }
  13.  
  14. void bubbleSort(long arr[], int n)
  15. {
  16. int i, j;
  17. for (i = 0; i < n-1; i++)
  18. {
  19. for (j = 0; j < n-i-1; j++)
  20. {
  21. if (arr[j] > arr[j+1])
  22. {
  23. swap(&arr[j], &arr[j+1]);
  24. }
  25. }
  26.  
  27. }
  28.  
  29. }
  30.  
  31.  
  32. int main() {
  33. int t;
  34. cin >> t;
  35. for(int c=0;c<t;c++)
  36. {
  37. // Input
  38. long B;
  39. int n;
  40. cin>> B >>n;
  41. int k[n];
  42. int max=0;
  43. for(int i=0;i<n;i++)
  44. {
  45. cin>>k[i];
  46. if(k[i]>max)
  47. {
  48. max=k[i];
  49. }
  50. }
  51. long val[n][max];
  52. for(int i=0;i<n;i++)
  53. {
  54. for(int j=0;j<k[i];j++)
  55. {
  56. cin>>val[i][j];
  57. }
  58. }
  59.  
  60. // Sorting
  61. for (int i=0;i<n;i++)
  62. {
  63. bubbleSort(val[i],k[i]);
  64. }
  65.  
  66. // Calculations
  67. cout << "Picking the cheapest components." << endl;
  68. multimap<long, int> pos;
  69. long b = B;
  70. for(int i=0;i<n;i++)
  71. {
  72. b = b - val[i][0];
  73. for(int j=k[i]-1;j>=0;j--)
  74. {
  75. val[i][j] = val[i][j] - val[i][0];
  76. if(val[i][j]>0)
  77. {
  78. pos.insert(pair<long, int>(val[i][j], i));
  79. }
  80. }
  81. }
  82. cout << "Remaining budget is: " << b << endl;
  83.  
  84. // Negative Check
  85. if (b<0)
  86. {
  87. cout << "There are no possible anwsers." << endl;
  88. cout << "0";
  89. return 0;
  90. }
  91. cout << "There are possible anwsers." << endl;
  92. cout << "Cost map is: " << endl;
  93. for (auto itr = pos.crbegin(); itr != pos.crend(); ++itr)
  94. {
  95. cout << itr->first << " " << itr->second << endl;
  96. }
  97. // Removing entries after budget change
  98. LOOP1:for (auto itr = pos.crbegin(); itr != pos.crend(); ++itr)
  99. {
  100. long key = itr->first;
  101. if (key>b){
  102. cout << "Removing entry bigger than remaining budget: ";
  103. cout << key << endl;
  104. pos.erase(key);
  105. goto LOOP1;
  106. }
  107. }
  108.  
  109. // Checking for solution
  110. cout << "Checking if map is empty: ";
  111. if (pos.empty())
  112. {
  113. cout << "(empty)" << endl;
  114. cout << "Budget spent is: " << endl;
  115. cout << B - b;
  116. return 0;
  117. }
  118. cout << "Map is not empty, more money can be spent." << endl;
  119.  
  120. // auto itr = pos.begin();
  121. // int key = itr->first;
  122. // int value = itr->second;
  123. // LOOP2:for (auto itr = pos.begin(); itr != pos.end(); ++itr)
  124. // {
  125. // int temp = itr->second;
  126. // if (temp == value)
  127. // {
  128. // pos.erase(itr);
  129. // goto LOOP2;
  130. // }
  131. // }
  132. // cout << "Removing " << key << " from line " << value << endl;
  133. // b = b - key;
  134. // for(int j=k[value]-1;j>=0;j--)
  135. // {
  136. // val[value][j] = val[value][j] - key;
  137. // if(val[value][j]>0)
  138. // {
  139. // pos.insert(pair<int, int>(val[value][j], value));
  140. // }
  141. // }
  142. // goto LOOP1;
  143. }
  144. return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement