josiftepe

Untitled

Oct 24th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <map>
  5. #include <set>
  6. using namespace std;
  7. int main() {
  8.     ios_base::sync_with_stdio(false);
  9.     int n;
  10.     cin >> n;
  11.     vector<int> v(n);
  12.     for(int &x : v) {
  13.         cin >> x;
  14.     }
  15.    
  16.     int i = 0, j = 0;
  17.     int ret = 0;
  18.     int subarray_length = 0;
  19.     map<int, int> cnt;
  20.     while(j < n) {
  21.         if(cnt.find(v[j]) == cnt.end() or cnt[v[j]] == 0) {
  22.             ++cnt[v[j]];
  23.             ++subarray_length;
  24.             ret = max(ret, subarray_length);
  25.             ++j;
  26.         }
  27.         else {
  28.             --subarray_length;
  29.             --cnt[v[i]];
  30.             ++i;
  31.         }
  32.     }
  33.     cout << ret << endl;
  34.    
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment