This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define SZ(a) (int)(a).size()
#define ALL(a) (a).begin(),(a).end()
signed main() {
int n;
cin >> n;
vector<vector<int>> pos(26);
for (int i = 1; i <= n; ++i) {
char c; cin >> c;
pos[int(c - 'a')].push_back(i);
}
int ans = 0;
for (int x = 0; x < 26; ++x) {
for (int y = 0; y < 26; ++y) {
if (x == y) {
continue;
}
if (!SZ(pos[x]) || !SZ(pos[y])) {
continue;
}
vector<int> a = {0};
{
int j = 0;
for (int i : pos[x]) {
while (j < SZ(pos[y]) && pos[y][j] < i) {
a.push_back(-1);
j++;
}
a.push_back(1);
}
while (j < SZ(pos[y])) {
a.push_back(-1);
j++;
}
}
int sz = SZ(a);
int mn = n + 1, lst = 0;
vector<int> pref(sz, 0);
for (int i = 1; i < sz; ++i) {
pref[i] = pref[i - 1] + a[i];
if (i > 1 && a[i] != a[i - 1]) {
for (int j = lst; j < i - 1; ++j) {
mn = min(mn, pref[j]);
}
lst = i - 1;
}
ans = max(ans, pref[i] - mn);
}
}
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |