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;
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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |