Advertisement
7oSkaaa

G - Batman Mex

Aug 13th, 2022
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
  6. #define cout_2d(vec, n, m) for(int i = 0; i < n; i++, cout << "\n") for(int j = 0; j < m && cout << vec[i][j] << " "; j++);
  7. #define fixed(n) fixed << setprecision(n)
  8. #define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  9. #define fill(vec, value) memset(vec, value, sizeof(vec));
  10. #define mul_mod(a, b, m) (((a % m) * (b % m)) % m)
  11. #define add_mod(a, b, m) (((a % m) + (b % m)) % m)
  12. #define all(vec) vec.begin(), vec.end()
  13. #define rall(vec) vec.rbegin(), vec.rend()
  14. #define sz(x) int(x.size())
  15. #define debug(x) cout << #x << ": " << (x) << "\n";
  16. #define fi first
  17. #define se second
  18. #define ll long long
  19. #define ull unsigned long long
  20. #define Mod  1'000'000'007
  21. #define OO 2'000'000'000
  22. #define EPS 1e-9
  23. #define PI acos(-1)
  24. template < typename T = int > using Pair = pair < T, T >;
  25. vector < string > RET = {"NO", "YES"};
  26.  
  27. template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
  28.     for (auto &x : v) in >> x;
  29.     return in;
  30. }
  31.  
  32. template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
  33.     for (const T &x : v) out << x << ' ';
  34.     return out;
  35. }
  36.  
  37. ll sum(ll n){
  38.     return n * (n - 1) / 2;
  39. }
  40.  
  41. void Solve(){
  42.     ll n, k;
  43.     cin >> n >> k;
  44.     vector < ll > nums(n), idx(n + 1, n + 1), freq(n + 1);
  45.     cin >> nums;
  46.     for(int i = 0; i < n; i++)
  47.         idx[nums[i]] = i + 1;
  48.     ll max_r = idx[0], min_l = idx[0];
  49.     freq[0] += sum(idx[0]) + sum(n - idx[0] + 1);
  50.     for(int i = 1; i <= n; i++){
  51.         max_r = max(max_r, idx[i - 1]);
  52.         min_l = min(min_l, idx[i - 1]);
  53.         if(max_r < idx[i])
  54.             freq[i] += (min_l * (idx[i] - max_r));
  55.         else if(min_l > idx[i])
  56.             freq[i] += ((min_l - idx[i]) * (n - max_r + 1));
  57.     }
  58.     for(int i = 0; i <= n; i++){
  59.         if(freq[i] < k)
  60.             k -= freq[i];
  61.         else
  62.             return cout << i << "\n", void();
  63.     }
  64. }
  65.  
  66.  
  67. int main(){
  68.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  69.     int t = 1;
  70.     cin >> t;
  71.     while(t--)
  72.         Solve();
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement