fork download
  1. #include <iostream>
  2. #include <queue>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main() {
  7. // your code goes here
  8. int n;
  9. cin>>n;
  10. int count=0;
  11. priority_queue<int, vector<int>, greater<int>>q;
  12. int min=1;//the next box needed to be removed
  13. bool ordered=true;
  14. for (int i=0; i<2*n; ++i){
  15. string s;
  16. cin>>s;
  17. if (s=="remove"){
  18. q.pop();
  19. ++min;
  20. }
  21. if (s=="add"){
  22. int k;
  23. cin>>k;
  24. if (!q.empty() && k>q.top())ordered=false;
  25. q.push(k);
  26. }
  27. if (ordered==false && q.top()==min){
  28. //reorder them now
  29. ordered=true;
  30. ++count;
  31. }
  32. }
  33. cout<<count;
  34. return 0;
  35. }
Success #stdin #stdout 0s 16064KB
stdin
7
add 3
add 2
add 1
remove
add 4
remove
remove
remove
add 6
add 7
add 5
remove
remove
remove
stdout
2