fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int solve(vector<int> v) {
  5. unordered_map<int, int> cnt;
  6. multiset<int> pq;
  7. const int n = v.size();
  8. int l = 0, r = 0;
  9. int ans = 0;
  10. while (r < n) {
  11. cnt[v[r]]++;
  12. pq.insert(cnt[v[r]]);
  13. while(*pq.rbegin() < r-l-2) {
  14. auto iter = pq.find(cnt[v[l]]);
  15. pq.erase(iter);
  16. cnt[v[l]]--;
  17. pq.insert(cnt[v[l]]);
  18. l++;
  19. }
  20. ans = max(ans, r-l+1);
  21. r++;
  22. }
  23. return ans;
  24. }
  25.  
  26. int main() {
  27. cout<<solve({-9, 8})<<endl;
  28. cout<<solve({1, 1, -10, 3, -10, 3, -10})<<endl;
  29. cout<<solve({2, 3, 3, 3, 3, 1})<<endl;
  30. cout<<solve({3, 3, 2, 1, 2, 2, 9, 3, 3})<<endl;
  31. return 0;
  32. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
2
6
6
6