Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- #include <map>
- #include <cstring>
- #include <algorithm>
- #include <stack>
- #include <queue>
- #include <fstream>
- using namespace std;
- typedef int ll;
- int main(){
- ios_base::sync_with_stdio(false);
- int n, k;
- cin >> n >> k;
- vector<int> v(n);
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- }
- vector<int> cnt(k, 0);
- int result = 0;
- multiset<int> ms;
- for(int i = 0; i < k; i++) {
- ms.insert(0);
- }
- int i = 0, j = 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]]);
- }
- if(*ms.begin() == 3 and *ms.rbegin() == 3) {
- result = max(result, j - i + 1);
- }
- j++;
- }
- else {
- if(v[i] < k) {
- ms.erase(ms.find(cnt[v[i]]));
- cnt[v[i]]--;
- ms.insert(cnt[v[i]]);
- }
- if(*ms.begin() == 3 and *ms.rbegin() == 3) {
- result = max(result, j - i + 1);
- }
- i++;
- }
- }
- cout << result << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement