Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- //#include <bits/stdc++.h>
- #include <fstream>
- #include <algorithm>
- #include <set>
- #include <vector>
- using namespace std;
- int main() {
- int n, k;
- cin >> n >> k;
- vector<int> v(n);
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- }
- multiset<int> ms;
- for(int i = 0; i < k; i++) {
- ms.insert(0);
- }
- vector<int> cnt(k, 0);
- int i = 0, j = 0;
- int res = 0;
- while(j < n) {
- if(*ms.begin() <= 3 and *ms.rbegin() <= 3) {
- if(v[j] < k) {
- ms.erase(ms.find(cnt[v[j]]));
- cnt[v[j]]++;
- ms.insert(cnt[v[j]]);
- }
- j++;
- if(*ms.begin() == 3 and *ms.rbegin() == 3) {
- res = max(res, j - i);
- }
- }
- else {
- if(v[i] < k) {
- ms.erase(ms.find(cnt[v[i]]));
- cnt[v[i]]--;
- ms.insert(cnt[v[i]]);
- }
- i++;
- if(*ms.begin() == 3 and *ms.rbegin() == 3) {
- res = max(res, j - i);
- }
- }
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement