#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
string s;
cin >> s;
int Q;
cin >> Q;
while (Q--) {
int L, R;
cin >> L >> R;
int k = R-L+1;
string t = s.substr(L-1, k);
vector<int> forward(k);
for (int i=0; i<k; i++) {
forward[i] = (i == 0 ? 0 : forward[i-1]) + (t[i] == 'C' ? 1 : -1);
}
map<int,int> first;
for (int i=k-1; i>=0; i--) {
first[forward[i]] = i;
}
int ans = 0;
for (int i=-1;; i--) {
if (first.count(i) == 0) break;
int idx = first[i];
t[idx] = 'x';
ans += 1;
}
// cout << t << "\n";
vector<int> backward(k);
for (int i=k-1; i>=0; i--) {
backward[i] = (i == k-1 ? 0 : backward[i+1]) + (t[i] == 'C' ? 1 : t[i] == 'T' ? -1 : 0);
}
int hi = 0;
for (int i=0; i<k; i++) {
hi = max(hi, -backward[i]);
}
cout << ans+hi << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
344 KB |
Output is correct |
2 |
Correct |
25 ms |
600 KB |
Output is correct |
3 |
Correct |
35 ms |
344 KB |
Output is correct |
4 |
Correct |
48 ms |
344 KB |
Output is correct |
5 |
Correct |
48 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
344 KB |
Output is correct |
2 |
Correct |
25 ms |
600 KB |
Output is correct |
3 |
Correct |
35 ms |
344 KB |
Output is correct |
4 |
Correct |
48 ms |
344 KB |
Output is correct |
5 |
Correct |
48 ms |
344 KB |
Output is correct |
6 |
Execution timed out |
3026 ms |
1876 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
344 KB |
Output is correct |
2 |
Correct |
25 ms |
600 KB |
Output is correct |
3 |
Correct |
35 ms |
344 KB |
Output is correct |
4 |
Correct |
48 ms |
344 KB |
Output is correct |
5 |
Correct |
48 ms |
344 KB |
Output is correct |
6 |
Execution timed out |
3026 ms |
1876 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |