#include <bits/stdc++.h>
using namespace std;
int solve_forward(const string &s) {
int balance = 0;
int removed = 0;
for (char c : s) {
if (c == 'C') balance++;
else balance--;
if (balance < 0) {
removed++;
balance++;
}
}
return removed;
}
int solve_backward(const string &s) {
int balance = 0;
int removed = 0;
for (int i = (int)s.size() - 1; i >= 0; i--) {
if (s[i] == 'C') balance++;
else balance--;
if (balance < 0) {
removed++;
balance++;
}
}
return removed;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
string S;
cin >> S;
int Q;
cin >> Q;
while (Q--) {
int L, R;
cin >> L >> R;
L--; R--;
string sub = S.substr(L, R - L + 1);
int fwd = solve_forward(sub);
int bwd = solve_backward(sub);
cout << max(fwd, bwd) << "\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... |