Advertisement
Guest User

Untitled

a guest
Nov 8th, 2012
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <set>
  8. #include <memory.h>
  9. #include <map>
  10. #include <math.h>
  11. #include <queue>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string>
  15. #include <iostream>
  16. #include <algorithm>
  17. #include <vector>
  18. #include <set>
  19. #include <memory.h>
  20. #include <map>
  21. #include <math.h>
  22. #include <queue>
  23. #include <stack>
  24. #include <complex>
  25. #include <list>
  26. #include <deque>
  27. #include <pthread.h>
  28. using namespace std;
  29.  
  30. typedef long long ll;
  31. typedef unsigned long long ull;
  32.  
  33. #define mp make_pair
  34. #define pb push_back
  35. #define sz(x) (int)x.size()
  36. #define X first
  37. #define Y second
  38. #define all(x) x.begin(),x.end()
  39.  
  40. class Task
  41. {
  42.     public:
  43.         Task(const int threadsNumber)
  44.         {
  45.             pthread_mutex_init(&mutex,NULL);       
  46.         }
  47.  
  48.         void push(const int value, const int timestamp)
  49.         {
  50.             pthread_mutex_lock(&mutex);
  51.             commands.pb(mp(timestamp, mp(1, value)));
  52.             pthread_mutex_unlock(&mutex);
  53.         }
  54.  
  55.         void pop(const int timestamp)
  56.         {
  57.             pthread_mutex_lock(&mutex);
  58.             commands.pb(mp(timestamp, mp(0, -1)));
  59.             pthread_mutex_unlock(&mutex);
  60.         }  
  61.  
  62.         void finish()
  63.         {
  64.             freopen("output.txt", "w", stdout);
  65.             vector<int> _stack;
  66.             sort(all(commands));
  67.             for (int i = 0; i < sz(commands); i++)
  68.             {
  69.                 if (commands[i].Y.X == 1)
  70.                 {
  71.                     _stack.pb(commands[i].Y.Y);
  72.                 }
  73.                 else
  74.                 {
  75.                     printf("%d\n", _stack[sz(_stack)-1]);
  76.                     _stack.pop_back();
  77.                 }
  78.             }
  79.         }
  80.  
  81.         vector< pair< int, pair< int, int> > > commands;
  82.         pthread_mutex_t mutex;
  83. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement