双指针

  1. for(int i = 0, j = 0; i < n; i ++)
  2. {
  3. while(j <= i && check(i, j)) j ++;
  4. //具体逻辑
  5. }
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <set>
  4. using namespace std;
  5. const int N = 1010;
  6. int n;
  7. int a[N];
  8. bool check(int l, int r)
  9. {
  10. set<int>s;
  11. for(int i = l; i <= r; i ++)
  12. s.insert(a[i]);
  13. if(s.size() != r - l + 1) return true;
  14. return false;
  15. }
  16. int main()
  17. {
  18. ios::sync_with_stdio(false);
  19. cin.tie(0);
  20. cout.tie(0);
  21. cin >> n;
  22. for(int i = 0; i < n; i ++)
  23. cin >> a[i];
  24. int ans = -1;
  25. for(int i = 0, j = 0; i < n; i ++)
  26. {
  27. while(j <= i && check(j, i)) j ++;
  28. ans = max(ans, i - j + 1);
  29. }
  30. cout << ans;
  31. // cout << a[1] << " " << a[2] << endl;
  32. // cout << check(1, 2);
  33. }