Advertisement
Josif_tepe

Untitled

Mar 2nd, 2023
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <map>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <stack>
  8. #include <queue>
  9. #include <fstream>
  10. using namespace std;
  11. typedef int ll;
  12.  
  13. int main(){
  14.     ios_base::sync_with_stdio(false);
  15.     int n, k;
  16.     cin >> n >> k;
  17.    
  18.     vector<int> v(n);
  19.     for(int i = 0; i < n; i++) {
  20.         cin >> v[i];
  21.     }
  22.    
  23.     vector<int> cnt(k, 0);
  24.     int result = 0;
  25.     multiset<int> ms;
  26.     for(int i = 0; i < k; i++) {
  27.         ms.insert(0);
  28.     }
  29.    
  30.     int i = 0, j = 0;
  31.     while(j < n) {
  32.         if(*ms.begin() <= 3 and *ms.rbegin() <= 3) {
  33.             if(v[j] < k) {
  34.                 ms.erase(ms.find(cnt[v[j]]));
  35.                 cnt[v[j]]++;
  36.                 ms.insert(cnt[v[j]]);
  37.             }
  38.             if(*ms.begin() == 3 and *ms.rbegin() == 3) {
  39.                 result = max(result, j - i + 1);
  40.             }
  41.             j++;
  42.         }
  43.         else {
  44.             if(v[i] < k) {
  45.                 ms.erase(ms.find(cnt[v[i]]));
  46.                 cnt[v[i]]--;
  47.                 ms.insert(cnt[v[i]]);
  48.             }
  49.             if(*ms.begin() == 3 and *ms.rbegin() == 3) {
  50.                 result = max(result, j - i + 1);
  51.             }
  52.             i++;
  53.         }
  54.     }
  55.    
  56.     cout << result << endl;
  57.     return 0;
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement