#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
char a[N];
vector<int>pos[30];
int n,val[N],maxPreSum[N],maxSufSum[N];
int calc(int a,int b) {
if (pos[a].size() == 0 || pos[b].size() == 0) return 0;
for (int i = 1; i <= n; i++) val[i] = 0;
for (auto x: pos[a]) val[x] = 1;
for (auto x: pos[b]) val[x] = -1;
maxPreSum[0] = maxSufSum[n + 1] = 0;
int sum = 0;
for (int i = 1; i <= n; i++) {
sum = max(sum + val[i],val[i]);
maxPreSum[i] = sum;
}
sum = 0;
for (int i = n; i > 0; i--) {
sum = max(sum + val[i],val[i]);
maxSufSum[i] = sum;
}
int ans = 0;
for (auto i: pos[b]) {
int tmp = maxPreSum[i - 1] + maxSufSum[i + 1] - 1;
tmp = max(tmp,maxPreSum[i - 1] - 1);
tmp = max(tmp,maxSufSum[i - 1] - 1);
ans = max(ans,tmp);
}
for (auto x: pos[b]) val[x] = 1;
for (auto x: pos[a]) val[x] = -1;
maxPreSum[0] = maxSufSum[n + 1] = 0;
sum = 0;
for (int i = 1; i <= n; i++) {
sum = max(sum + val[i],val[i]);
maxPreSum[i] = sum;
}
sum = 0;
for (int i = n; i > 0; i--) {
sum = max(sum + val[i],val[i]);
maxSufSum[i] = sum;
}
for (auto i: pos[a]) {
int tmp = maxPreSum[i - 1] + maxSufSum[i + 1] - 1;
tmp = max(tmp,maxPreSum[i - 1] - 1);
tmp = max(tmp,maxSufSum[i - 1] - 1);
ans = max(ans,tmp);
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) pos[a[i] - 'a'].push_back(i);
int ans = 0;
for (int i = 0; i < 26; i++)
for (int j = i + 1; j < 26; j++)
if (i != j) ans = max(ans,calc(i,j));
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
6996 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
189 ms |
11376 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
32 ms |
6784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1052 ms |
18012 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1032 ms |
17824 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1097 ms |
18084 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1042 ms |
17772 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |