Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement