Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <set>
  4.  
  5. using namespace std;
  6.  
  7. struct comparator {
  8. bool operator() (pair<int,int> x, pair<int,int> y){
  9. return x.second > y.second;
  10. }
  11. };
  12.  
  13. void reorganizeStack(stack<pair<int, int> > *pilha){
  14. set<pair<int,int>, comparator> auxSet;
  15. while(!(*pilha).empty()){
  16. auxSet.insert((*pilha).top());
  17. (*pilha).pop();
  18. }
  19. set<pair<int, int> >::iterator it;
  20. for (it = auxSet.begin(); it != auxSet.end(); it++){
  21. (*pilha).push(*it);
  22. }
  23. }
  24.  
  25. int main(){
  26. char l;
  27. int f, d, q, n;
  28. pair<int, int> aux;
  29. stack<pair<int,int> > fenos;
  30. cin >> f;
  31. for (int i = 0; i < f; i++){
  32. cin >> d >> q;
  33. aux.first = d;
  34. aux.second = q;
  35. if(aux.first > 0 && aux.second > 0){
  36. if (!fenos.empty() && aux.second == fenos.top().second){
  37. fenos.top().first += aux.first;
  38. } else {
  39. fenos.push(aux);
  40. }
  41. reorganizeStack(&fenos);
  42. }
  43. }
  44.  
  45. cin >> n;
  46. for (int i = 0; i < n; i++){
  47. cin >> l;
  48. if (l == 'C') {
  49. cin >> q;
  50. if(!fenos.empty() && q > 0){
  51. if (fenos.top().first > q) {
  52. fenos.top().first -= q;
  53. } else {
  54. while (!fenos.empty() && fenos.top().first < q) {
  55. q -= fenos.top().first;
  56. if(!fenos.empty()) fenos.pop();
  57. if (fenos.empty()) {
  58. break;
  59. }
  60. }
  61. if(!fenos.empty()) {
  62. fenos.top().first -= q;
  63. if(!fenos.empty() && fenos.top().first == 0) fenos.pop();
  64. }
  65. }
  66. }
  67. } else {
  68. cin >> q >> d;
  69. aux.first = q;
  70. aux.second = d;
  71. if(aux.first > 0 || aux.second > 0){
  72. if (!fenos.empty() && aux.second == fenos.top().second){
  73. fenos.top().first += aux.first;
  74. } else {
  75. fenos.push(aux);
  76. }
  77. reorganizeStack(&fenos);
  78. }
  79. }
  80. }
  81. if (fenos.empty()) cout << "Sem estoque" <<endl;
  82. else while (!fenos.empty()){
  83. cout << fenos.top().first << " " << fenos.top().second << endl;
  84. fenos.pop();
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement