#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 |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
1364 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
658 ms |
8316 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
411 ms |
7484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
651 ms |
8464 KB |
Output is correct |
2 |
Correct |
526 ms |
6588 KB |
Output is correct |
3 |
Correct |
165 ms |
8324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
611 ms |
8472 KB |
Output is correct |
2 |
Correct |
75 ms |
18312 KB |
Output is correct |
3 |
Correct |
118 ms |
10076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
638 ms |
8356 KB |
Output is correct |
2 |
Correct |
45 ms |
22100 KB |
Output is correct |
3 |
Correct |
208 ms |
9352 KB |
Output is correct |