fork download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6.  
  7. template <class T>
  8. using ord_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  9.  
  10. typedef long long ll;
  11.  
  12. #define endl '\n'
  13. #define pb push_back
  14. #define eb emplace_back
  15. #define ff first
  16. #define ss second
  17.  
  18. const int MAXN = 2e5 + 10;
  19. ll n, k, a[MAXN], prefix[MAXN+1];
  20.  
  21. ll count(ll m) {
  22. prefix[0] = 0;
  23. for (int i = 1; i <= n; i++) {
  24. // Contar elementos <= m (mediana <= m)
  25. prefix[i] = prefix[i-1] + (a[i-1] <= m ? 1 : -1);
  26. }
  27. ll ans = 0;
  28. ord_set<pair<ll,int>> s;
  29. s.insert({0, -1}); // prefixo vazio
  30. for (int i = 1; i <= n; i++) {
  31. // quantos prefixos anteriores <= prefix[i] ?
  32. ans += s.order_of_key({prefix[i] + 1, -1});
  33. s.insert({prefix[i], i});
  34. }
  35. return ans;
  36. }
  37.  
  38. bool check(ll m) {
  39. if (count(m) >= k) return true;
  40. else return false;
  41. }
  42.  
  43. signed main() {
  44. ios_base::sync_with_stdio(0);cin.tie(0);
  45. cin >> n >> k;
  46. for (int i = 0; i < n; i++) cin >> a[i];
  47. ll l = 1, r = 1e9 + 5;
  48. while (l < r) {
  49. ll m = l + (r - l) / 2;
  50. if (check(m)) r = m;
  51. else l = m + 1;
  52. }
  53. cout << l << endl;
  54. }
  55.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
1