Advertisement
Kali_prasad

distinct elements in the window of k size

Apr 2nd, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target ("sse4")
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8. typedef pair<int, int> pii;
  9. typedef pair<string,int> psi;
  10. typedef vector<int> vi;
  11. typedef vector<string> vs;
  12. typedef vector<ll> vll;
  13. typedef vector<vector<int>> vvi;
  14. typedef vector<vector<string>> vvs;
  15. typedef vector<vector<ll>> vvll;
  16.  
  17. #define FOR(i, a, b) for (auto i=a; i<=(b); i++)
  18. #define FORd(i,b,a) for (int i =b; i >= a; i--)
  19. #define sz(x) (int)(x).size()
  20. #define mp make_pair
  21. #define pb push_back
  22. #define f first
  23. #define s second
  24. #define ins insert
  25.  
  26. const int MOD = 1000000007;
  27. //type functions here
  28.  
  29. //terminated due to timeout
  30.  
  31. int main() {
  32.     int tc;
  33.     cin>>tc;
  34.     FOR(i,1,tc){
  35.     vector<int> v;
  36.     int n,k;
  37.     cin>>n>>k;
  38.     FOR(j,1,n)
  39.     {
  40.      int temp;
  41.      cin>>temp;
  42.      v.pb(temp);
  43.     }
  44.     int min=*min_element(v.begin(),v.end());
  45.     int offset=0;
  46.     if(min<0)
  47.     offset=abs(min);
  48.     map<int,int> m;
  49.     FOR(i,0,n-1)
  50.     {
  51.        
  52.         m[v[i]+offset]++;
  53.         if(i>=k) m[v[i-k]+offset]--;
  54.         if(i>=k-1)
  55.         {
  56.             int c=0;
  57.             for(auto x:m)
  58.             if(x.s>0) c++;
  59.             cout<<c<<" ";
  60.         }
  61.      
  62.  
  63.     }
  64.         cout<<endl;
  65.     }
  66.    
  67.     return 0;
  68. }
  69.  
  70.  
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement