D_L3

Подмасив

Jan 25th, 2024
977
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <unordered_map>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. int main() {
  12.     unordered_map<int, int> map;
  13.     int start = 0;
  14.     int longest = 0;
  15.    
  16.     int n, a;
  17.     cin >> n;
  18.    
  19.     for(int i = 0; i < n; i++){
  20.         cin >> a;
  21.         if(map.count(a) == 0 || map[a] < start){
  22.             map[a] = i;
  23.             continue;
  24.         }
  25.        
  26.         if(i - start> longest)
  27.             longest = i - start;
  28.        
  29.         start = map[a] + 1;
  30.         map[a] = i;
  31.     }
  32.     if(n - start > longest)
  33.         longest = n - start;
  34.     cout << longest;
  35.     return 0;
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment