fork 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. typedef pair<int, int> pii;
  50.  
  51.  
  52.  
  53.  
  54.  
  55. vector<int> a;
  56.  
  57.  
  58. //? Advanced PBDS
  59.  
  60. vector<double> consistency(int n, int k) {
  61. vector<double> ans;
  62.  
  63. ordered_set<pair<int, int>> window;
  64.  
  65. int s = 0, e = 0;
  66. while(e<n) {
  67. window.insert({a[e], e});
  68.  
  69. if (e-s+1 < k) {
  70. e++;
  71. }
  72. else{
  73. if (k & 1) {
  74. auto it = window.find_by_order(k / 2);
  75. ans.push_back((double)it->first);
  76. } else {
  77. auto it1 = window.find_by_order(k / 2 - 1);
  78. auto it2 = window.find_by_order(k / 2);
  79. double median = (it1->first + 0.0 + it2->first) / 2.0;
  80. ans.push_back(median);
  81. }
  82. window.erase({a[s], s});
  83. s++;
  84. e++;
  85. }
  86. }
  87.  
  88. return ans;
  89. }
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. vector<double> practice(int n, int k) {
  114.  
  115. return {};
  116.  
  117. }
  118.  
  119.  
  120.  
  121.  
  122. void solve() {
  123.  
  124. int n, k;
  125. cin>>n >> k;
  126.  
  127. a.resize(n);
  128. for(int i=0; i<n; i++) cin >> a[i];
  129.  
  130. auto ans1 = consistency(n, k);
  131. for(auto& t : ans1 ) cout << t << " "; cout << " : ";
  132.  
  133.  
  134.  
  135.  
  136. // auto p = practice(n, k);
  137. // cout << " -> ";
  138. // for(auto& it : ans1 ) {
  139. // cout << it << " ";
  140. // }
  141. // cout << endl;
  142.  
  143.  
  144.  
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151. int32_t main() {
  152. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  153.  
  154. int t = 1;
  155. cin >> t;
  156. while (t--) {
  157. solve();
  158. }
  159.  
  160. return 0;
  161. }
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    :   2 3 3 3 2 3 2    :   1 2 2 0    :