Created
October 5, 2019 02:52
-
-
Save pablohildo/1ad3d94a8f47a850366cc3abc545f180 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stack> | |
#include <set> | |
using namespace std; | |
struct comparator { | |
bool operator() (pair<int,int> x, pair<int,int> y){ | |
return x.second > y.second; | |
} | |
}; | |
void reorganizeStack(stack<pair<int, int> > *pilha){ | |
set<pair<int,int>, comparator> auxSet; | |
while(!(*pilha).empty()){ | |
auxSet.insert((*pilha).top()); | |
(*pilha).pop(); | |
} | |
set<pair<int, int> >::iterator it; | |
for (it = auxSet.begin(); it != auxSet.end(); it++){ | |
(*pilha).push(*it); | |
} | |
} | |
int main(){ | |
char l; | |
int f, d, q, n; | |
pair<int, int> aux; | |
stack<pair<int,int> > fenos; | |
cin >> f; | |
for (int i = 0; i < f; i++){ | |
cin >> d >> q; | |
aux.first = d; | |
aux.second = q; | |
if(aux.first > 0 && aux.second > 0){ | |
if (!fenos.empty() && aux.second == fenos.top().second){ | |
fenos.top().first += aux.first; | |
} else { | |
fenos.push(aux); | |
} | |
reorganizeStack(&fenos); | |
} | |
} | |
cin >> n; | |
for (int i = 0; i < n; i++){ | |
cin >> l; | |
if (l == 'C') { | |
cin >> q; | |
if(!fenos.empty() && q > 0){ | |
if (fenos.top().first > q) { | |
fenos.top().first -= q; | |
} else { | |
while (!fenos.empty() && fenos.top().first < q) { | |
q -= fenos.top().first; | |
if(!fenos.empty()) fenos.pop(); | |
if (fenos.empty()) { | |
break; | |
} | |
} | |
if(!fenos.empty()) { | |
fenos.top().first -= q; | |
if(!fenos.empty() && fenos.top().first == 0) fenos.pop(); | |
} | |
} | |
} | |
} else { | |
cin >> q >> d; | |
aux.first = q; | |
aux.second = d; | |
if(aux.first > 0 || aux.second > 0){ | |
if (!fenos.empty() && aux.second == fenos.top().second){ | |
fenos.top().first += aux.first; | |
} else { | |
fenos.push(aux); | |
} | |
reorganizeStack(&fenos); | |
} | |
} | |
} | |
if (fenos.empty()) cout << "Sem estoque" <<endl; | |
else while (!fenos.empty()){ | |
cout << fenos.top().first << " " << fenos.top().second << endl; | |
fenos.pop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment