fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define int long long
  5. #define yes cout << "YES\n";
  6. #define no cout << "NO\n";
  7.  
  8.  
  9. void FastIO(){
  10. ios_base::sync_with_stdio(false);
  11. cin.tie(nullptr);
  12. cout.tie(nullptr);
  13. }
  14. /// Playlist
  15. void solve(){
  16. int n; cin >> n;
  17.  
  18. vector<int> vec(n);
  19. map<int,int>freq;
  20.  
  21. for(int i = 0; i < n; i++){
  22. cin >> vec[i];
  23. }
  24.  
  25. int l = 0, r = 0, ans = 0;
  26.  
  27. while(r < n){
  28. freq[vec[r]]++;
  29.  
  30. while(freq[vec[r]] > 1){
  31. freq[vec[l]]--;
  32. l++;
  33. }
  34. ans = max(ans, r-l+1);
  35. r++;
  36. }
  37. cout << ans;
  38. }
  39.  
  40. signed main(){
  41. FastIO();
  42.  
  43. int t = 1;
  44. //cin >> t;
  45.  
  46. while(t--){
  47. solve();
  48. }
  49. return 0;
  50. }
Success #stdin #stdout 0.01s 5272KB
stdin
8
1 2 1 3 2 7 4 2

stdout
5