Tranvick

Queue+min

Mar 6th, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <stack>
  2. #include <iostream>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. stack <pair<int,int> > s1,s2;
  8.  
  9. void push(int x){
  10.     int minim=s1.empty()?x:min(x,s1.top().second);
  11.     s1.push(make_pair(x,minim));
  12. }
  13.  
  14. int getmin(void){
  15.     if (s1.empty() || s2.empty()) return s1.empty()?s2.top().second:s1.top().second;
  16.     return min(s1.top().second,s2.top().second);
  17. }
  18.  
  19. int pop(void){
  20.     if (s2.empty())
  21.         while (!s1.empty()){
  22.             int x=s1.top().first;
  23.             s1.pop();
  24.             int minim=s2.empty()?x:min(x,s2.top().second);
  25.             s2.push(make_pair(x,minim));
  26.         }
  27.     int x=s2.top().first;
  28.     s2.pop();
  29.     return x;
  30. }
  31.  
  32. bool empty(void){
  33.     return s1.empty() && s2.empty();
  34. }
  35.  
  36. int main(void){
  37.     int n,m;
  38.     cin>>n>>m;
  39.     for (int i=1;i<=m;i++){
  40.         int x;
  41.         cin>>x;
  42.         push(x);
  43.     }
  44.     for (int i=m+1;i<=n;i++){
  45.         int x;
  46.         cin>>x;
  47.         cout<<getmin()<<endl;
  48.         push(x);
  49.         pop();
  50.     }
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment