Advertisement
archit1997

DSA STL SBUPRNJL solution

Aug 16th, 2020
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. #include <functional>
  5.  
  6. using namespace std;
  7. using namespace __gnu_pbds;
  8.  
  9. #define ll      long long int
  10. #define ld      long double
  11. #define line    cout<<"-------------"<<endl;
  12. #define F       first
  13. #define S       second
  14. #define P       pair<ll,ll>
  15. #define PP      pair<pair<ll,ll>,ll>
  16. #define V       vector<ll>
  17. #define VP      vector<pair<ll,ll>>
  18. #define VS      vector<string>
  19. #define VV      vector<vector<ll>>
  20. #define VVP     vector<vector<pair<ll,ll>>>
  21. #define pb      push_back
  22. #define pf      push_front
  23. #define PQ      priority_queue<ll>
  24. #define PQ_G    priority_queue<ll,vector<ll>,greater<ll>>
  25. #define line    cout<<"-------------"<<endl;
  26. #define mod     1000000007
  27. #define inf     1e18
  28.  
  29. #define setbits(x)      __builtin_popcount(x)
  30. #define zerobits(x)     __builtin_ctzll(x)
  31. #define ps(x,y)         fixed<<setprecision(y)<<x
  32. #define w(x)            ll x; cin>>x; while(x--)
  33. #define FOR(i,a,b)      for(ll i=a;i<b;i++)
  34. #define ma(arr,n,type)  type *arr=new type[n]
  35. //here we are using an ordered set of pairs
  36. #define pbds tree<P,null_type,less<P>,rb_tree_tag,tree_order_statistics_node_update>
  37.  
  38. void init()
  39. {
  40.     ios_base::sync_with_stdio(false);
  41.     cin.tie(NULL); cout.tie(NULL);
  42.    
  43.     //freopen("input.txt","r",stdin);
  44.     //freopen("output.txt","w",stdout);
  45. }
  46.  
  47. int main()
  48. {
  49.    
  50. init();
  51.  
  52.     ll t;   cin>>t;
  53.    
  54.     while(t--){
  55.         ll n,k; cin>>n>>k;
  56.        
  57.         V v(n);
  58.        
  59.         FOR(i,0,n)
  60.             cin>>v[i];
  61.            
  62.         ll ans=0;
  63.         //now iterate over all possible subarrays
  64.         FOR(i,0,n){
  65.             V hash(2001,0);
  66.             pbds st;
  67.             FOR(j,i,n){
  68.                 //This is the correct way to insert duplicates in a set
  69.                 //by storing the element along with its frequency
  70.                 st.insert({v[j],hash[v[j]]++});
  71.                 ll m=ceil(((double)k)/(j-i+1));
  72.                 ll pos=ceil((double)k/m);
  73.                 pos--;
  74.                 auto it=st.find_by_order(pos);
  75.                 ll val=(*it).F;
  76.                 //finding f which is the frequency of x in S(the subarray)
  77.                 ll f =hash[val];
  78.                 if(hash[f]>0)
  79.                     ans++;
  80.             }            
  81.         }
  82.        
  83.         cout<<ans<<"\n";
  84.     }
  85.    
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement