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 NAME "task"
const int N = 1e6 + 5;
const int inf = 0x3f3f3f3f;
vector<int>pos[30];
int n,ans,cnt[N],pre[N];
char s[N];
void calc(int a,int b) {
vector<int>vec;
int i = 0,j = 0;
while (i < (int)pos[a].size() && j < (int)pos[b].size()) {
if (pos[a][i] < pos[b][j]) {
vec.push_back(1);
i++;
}
else {
vec.push_back(-1);
j++;
}
}
while (i < (int)pos[a].size()) {
vec.push_back(1);
i++;
}
while (j < (int)pos[b].size()) {
vec.push_back(-1);
j++;
}
int m = vec.size();
pre[0] = 0,cnt[0] = 0;
for (int i = 1; i <= m; i++) {
if (vec[i - 1] == -1) cnt[i] = cnt[i - 1] + 1;
else cnt[i] = cnt[i - 1];
pre[i] = pre[i - 1] + vec[i - 1];
}
int Min = inf; j = 0;
for (int i = 1; i <= m; i++) {
while (j < i && cnt[i] - cnt[j] > 0) {
Min = min(Min,pre[j]);
j++;
}
ans = max(ans,pre[i] - Min);
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i];
pos[s[i] - 'a'].push_back(i);
}
for (int i = 0; i < 26; i++)
for (int j = 0; j < 26; j++)
if (i != j && pos[i].size() && pos[j].size()) calc(i,j);
cout << ans << "\n";
}
# | 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... |