Skip to content

Instantly share code, notes, and snippets.

@KirillLykov
Last active January 26, 2018 20:51
Show Gist options
  • Select an option

  • Save KirillLykov/fdd412aebc12b34ef38071ef63dec689 to your computer and use it in GitHub Desktop.

Select an option

Save KirillLykov/fdd412aebc12b34ef38071ef63dec689 to your computer and use it in GitHub Desktop.
codeforces 333, div2 b
// Kinda long solution
// Complexity - nlogn
#include <bits/stdc++.h>
using namespace std;
int getLargestAConst(const vector<int>& a) {
int maxL = 0;
int l = 0, r = 0;
map<int, int> subseq;
while (r != a.size()) {
if (l == r) {
if (subseq.count(a[r]) == 0)
subseq.emplace(a[r], 1);
else
++subseq[a[r]];
++r;
} else if (subseq.rbegin()->first - subseq.begin()->first <= 1) {
maxL = max(maxL, r - l);
if (subseq.count(a[r]) == 0)
subseq.emplace(a[r], 1);
else
++subseq[a[r]];
++r;
} else {
--subseq[a[l]];
if (subseq[a[l]] == 0)
subseq.erase(subseq.find(a[l]));
++l;
}
}
if (subseq.rbegin()->first - subseq.begin()->first <= 1)
return max(maxL, r - l);
return maxL;
}
int main(int, char**) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
cout << getLargestAConst(a) << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment