Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- 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 result = 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) {
- result = max(result, 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) {
- result = max(result, j - i);
- }
- }
- }
- cout << result << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement