fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. main(){
  5. int n,k;
  6. cin>>n>>k;
  7. vector<int>v(n);
  8. for(auto &it:v)cin>>it;
  9. unordered_map<int,int>mp;
  10. int i=0,j=0,subarray=0;
  11. while(j<n){
  12. mp[v[j]]++;
  13. while(mp.size()>k){
  14. if(mp[v[i]]==1)mp.erase(v[i]);
  15. else mp[v[i]]--;
  16. i++;
  17. }
  18. subarray+=j-i+1;
  19. j++;
  20. }
  21. cout<<subarray<<"\n";
  22. }
Success #stdin #stdout 0.01s 5280KB
stdin
5 2
1 2 2 3 4
stdout
11