fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long int
  4. const int M = 1e9 + 7;
  5.  
  6. // Sep 1 Rippling OA Code
  7.  
  8.  
  9. int main()
  10. {
  11. ios_base::sync_with_stdio(false);
  12. cin.tie(NULL);
  13. int n, k;
  14. cin >> n >> k;
  15. vector<int> v(n);
  16. for (int i = 0; i < n; i++)
  17. cin >> v[i];
  18. map<int, vector<int>> mp;
  19. for (int i = 0; i < n; i++)
  20. {
  21. mp[v[i]].push_back(i);
  22. }
  23. int ans=0;
  24.  
  25. for (auto it : mp)
  26. {
  27. vector<int> arr = it.second;
  28. int sz = arr.size();
  29. int i = 0, j = 0;
  30. int cnt = 0;
  31. int rem = k;
  32. int ma = 0;
  33. while (j < sz)
  34. {
  35. if (j + 1 < sz)
  36. {
  37. while (rem < arr[j + 1] - arr[j] - 1)
  38. {
  39. rem += arr[i + 1] - arr[i] - 1;
  40. cnt--;
  41. i++;
  42. }
  43. rem -= (arr[j + 1] - arr[j] - 1);
  44. }
  45.  
  46. cnt++;
  47. ma=max(ma,cnt);
  48. j++;
  49. }
  50. ans=max(ans,ma);
  51. }
  52. cout<<ans<<endl;
  53. return 0;
  54. }
Success #stdin #stdout 0.01s 5328KB
stdin
9 2
2 3 2 3 2 3 2 2 2
stdout
5