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 __gnu_pbds;
  5. #define int long long
  6. #define oset tree < pair<int, int> , null_type , less<pair<int, int>> , rb_tree_tag , tree_order_statistics_node_update >
  7. using namespace std;
  8. const int N = 1e5;
  9. signed main(){
  10. //You are given an array A of length N (N<=2000)
  11. //You have to consider all contigious subarrays (O(N^2) is possible)
  12. //K is given as input
  13. //M x length of array >= k
  14. //M = ceil(k/length of array)
  15. //X = kth smallest element of B
  16. //The kth smallest element of B would be the ceil(K/M)th smallest element of S?
  17. //S = (1, 2, 3, 4, 5), M = 2 = (1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
  18. //B = (1, 1, 2, 2, 3, 3, 4, 4, 5, 5), find the 9th smallest element in B
  19. //The kth smallest element in B, would be the Yth smallest element in S
  20. //What is Y? Y = ceil(K/M)
  21. //ceil(9/2) = 5
  22. //5th smallest element of S, is 5 again
  23. //F is the frequency of X in S (the original subarray, before concatenation)
  24. //The subarray S is beautiful if F also occurs in S
  25. //We need to output the number of beautiful subarrays
  26. ios::sync_with_stdio(false);
  27. cin.tie(0);
  28. cout.tie(0);
  29. int t;
  30. cin>>t;
  31. while(t--){
  32. int n, k;
  33. cin>>n>>k;
  34. int ans = 0;
  35. int a[n];
  36. for(int i = 0;i<n;i++) cin>>a[i];
  37. for(int i = 0;i<n;i++){
  38. int freq[2001];
  39. for(int i = 0;i<=2000;i++) freq[i] = 0;
  40. oset s;
  41. for(int j = i;j<n;j++){ //iterating over all subarrays i, i+1, i+2, .., j
  42. int m = ceil(((double)k)/(j - i + 1));
  43. int k1 = ceil(((double)k)/m);
  44. k1--; //we use 0 based indexing
  45. s.insert({a[j], freq[a[j]]++});
  46. auto it = s.find_by_order(k1);
  47. int element = (*it).first;
  48. int frequency = freq[element];
  49. if(freq[frequency]>0) ans++; //subarray is beautiful
  50. //We have a subarray i.. j
  51. //How do we find the kth smallest number in O(1) or O(LogN)?
  52. }
  53. }
  54. cout<<ans<<"\n";
  55. }
  56. }
  57. //acdabcd
Success #stdin #stdout 0s 4260KB
stdin
Standard input is empty
stdout
Standard output is empty