fork(1) download
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. //_ ******************* Policy Based Data Structure ****************************
  5.  
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8.  
  9. using namespace std;
  10. using namespace __gnu_pbds;
  11.  
  12.  
  13. template <class T>
  14. using ordered_set = tree<
  15. T,
  16. null_type,
  17. less<T>,
  18. rb_tree_tag,
  19. tree_order_statistics_node_update
  20. >;
  21.  
  22. //_ ****************************************************************************
  23.  
  24.  
  25. #define int long long int
  26. #define double long double
  27. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  28. inline int power(int a, int b) {
  29. int x = 1;
  30. while (b) {
  31. if (b & 1) x *= a;
  32. a *= a;
  33. b >>= 1;
  34. }
  35. return x;
  36. }
  37.  
  38.  
  39. const int M = 1000000007;
  40. const int N = 3e5+9;
  41. const int INF = 2e9+1;
  42. const int LINF = 2000000000000000001;
  43.  
  44. //_ ***************************** START Below *******************************
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. vector<int> a;
  55.  
  56. vector<double> consistency1(int n, int k) {
  57. vector<double> ans;
  58.  
  59. set<pair<int,int>> rightMin; // max heap
  60. set<pair<int,int>, greater<pair<int,int>>> leftMax; //* min heap
  61.  
  62.  
  63. int s = 0, e = 0;
  64. while(e<n){
  65. leftMax.insert({a[e], e});
  66.  
  67. //* rebalance now
  68. if(leftMax.size() > k/2){
  69. auto x = *leftMax.begin();
  70. leftMax.erase(x);
  71. rightMin.insert(x);
  72. } if(e-s+1 < k){
  73. e++;
  74. }
  75. else{
  76. int x = (*leftMax.begin()).first;
  77. int y = (*rightMin.begin()).first;
  78. double median = (x + 0.0 +y)/2.0;
  79. if(k & 1){
  80. median = y;
  81. }
  82. ans.push_back(median);
  83. leftMax.erase({a[s], s});
  84. rightMin.erase({a[s], s});
  85.  
  86. //* rebalance now
  87. if(leftMax.size() < k/2){
  88. auto y = *rightMin.begin();
  89. rightMin.erase(y);
  90. leftMax.insert(y);
  91. }
  92. s++;
  93. e++;
  94. }
  95. }
  96. return ans;
  97. }
  98.  
  99.  
  100. //? Using Ordered set from Policy based ds
  101.  
  102. vector<double> consistency2(int n, int k) {
  103. vector<double> ans;
  104.  
  105. ordered_set<pair<int, int>> window;
  106.  
  107. int s = 0, e = 0;
  108. while(e<n) {
  109. window.insert({a[e], e});
  110.  
  111. if (e-s+1 < k) {
  112. e++;
  113. }
  114. else{
  115. if (k & 1) {
  116. auto it = window.find_by_order(k / 2);
  117. ans.push_back((double)it->first);
  118. } else {
  119. auto it1 = window.find_by_order(k / 2 - 1);
  120. auto it2 = window.find_by_order(k / 2);
  121. double median = (it1->first + 0.0 + it2->first) / 2.0;
  122. ans.push_back(median);
  123. }
  124. window.erase({a[s], s});
  125. s++;
  126. e++;
  127. }
  128. }
  129.  
  130. return ans;
  131. }
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. vector<double> practice(int n, int k) {
  151.  
  152. return {};
  153.  
  154. }
  155.  
  156. void solve() {
  157.  
  158. int n, k;
  159. cin>>n >> k;
  160.  
  161. a.resize(n);
  162. for(int i=0; i<n; i++) cin >> a[i];
  163.  
  164. auto ans1 = consistency1(n, k);
  165. for(auto& it : ans1 ) {
  166. cout << it << " ";
  167. }
  168. cout << " : ";
  169.  
  170. auto ans2 = consistency2(n, k);
  171. for(auto& it : ans2 ) {
  172. cout << it << " ";
  173. }
  174. cout << endl;
  175.  
  176.  
  177.  
  178. // auto p = practice(n, k);
  179. // cout << " -> ";
  180. // for(auto& it : ans1 ) {
  181. // cout << it << " ";
  182. // }
  183. // cout << endl;
  184.  
  185.  
  186.  
  187. }
  188.  
  189.  
  190.  
  191.  
  192.  
  193. int32_t main() {
  194. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  195.  
  196. int t = 1;
  197. cin >> t;
  198. while (t--) {
  199. solve();
  200. }
  201.  
  202. return 0;
  203. }
Success #stdin #stdout 0s 5320KB
stdin
3
8 3
1 3 -1 -3 5 3 6 7
9 3
1 2 3 4 2 3 1 4 2
6 3
1 1 3 2 0 0
stdout
1 -1 -1 3 5 6    :   1 -1 -1 3 5 6 
2 3 3 3 2 3 2    :   2 3 3 3 2 3 2 
1 2 2 0    :   1 2 2 0