Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #pragma warning (disable : 4996)
  2. #pragma comment(linker, "/STACK:36777216")
  3.  
  4. #include <stdlib.h>
  5. #include <iostream>
  6. #include <vector>
  7. #include <string>
  8. #include <assert.h>
  9. #include <stack>
  10. #include <algorithm>
  11. #include <ios>
  12. #include <iostream>
  13. #include <fstream>
  14. #include <iomanip>
  15. #include <queue>
  16. #include <set>
  17. #include <functional>
  18. #include <cmath>
  19. #include <sstream>
  20. #include <map>
  21. #include <memory.h>
  22. #include <stdio.h>
  23. #include <cassert>
  24. #include <string.h>
  25. #include <deque>
  26. #include <ctime>
  27. #include <list>
  28. #include <unordered_set>
  29. #include <unordered_map>
  30. #include <limits>
  31.  
  32. #define forn(i , n) for (int i = 0; i < n; ++i)
  33. #define down(i, n) for (int i = (n) - 1; i >= 0; --i)
  34.  
  35. using namespace std;
  36.  
  37. typedef unsigned long long int u64;
  38. typedef long long int i64;
  39. typedef vector<int> vint;
  40. typedef vector<i64> vi64;
  41. typedef pair<int, int> pint;
  42. typedef pair<i64, i64> pi64;
  43.  
  44. #define FILE_NAME "file"
  45. #define CONTEST "hard"
  46. #define M_PI 3.14159265358979323846
  47. #define ALL(a) (a).begin(), (a).end()
  48.  
  49. #define N 100000000
  50.  
  51. int main()
  52. {
  53.     clock_t start = clock();
  54.     srand(13);
  55.     srand(start);
  56.     cout << fixed << setprecision(10);
  57.     ios_base::sync_with_stdio(false);
  58.     cin.tie(NULL);
  59. #ifdef FILE_INPUT
  60.     freopen("file.in", "r", stdin);
  61.     freopen("file.out", "w", stdout);
  62.  
  63. #endif // FILE_INPUT
  64.  
  65.     unsigned k;
  66.     cin >> k;
  67.  
  68.     int currAnswer = INT_MIN;
  69.     set<int> s; // red-black tree https://en.cppreference.com/w/cpp/container/set.
  70.  
  71.     while (!cin.eof()) // enf of stream.
  72.     {
  73.         int c;
  74.         cin >> c;
  75.         if (c < currAnswer && s.size() == k)
  76.         {
  77.             // do nothing, O(1).
  78.         }
  79.         else
  80.         {
  81.             s.insert(c); // O(ln(n));
  82.  
  83.             if (s.size() > k)
  84.             {
  85.                 auto currMin = s.begin();  // O(ln(n));
  86.                 s.erase(currMin);  // O(ln(n));
  87.             }
  88.  
  89.             // if (s.size() == k) it depends on what you want to see before you have k elements.
  90.             currAnswer = *s.begin(); // min element of the set, O(ln(n));
  91.         }
  92.         cout << currAnswer << " ";
  93.  
  94.     }
  95.  
  96.  
  97.  
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement