#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
int n;
string s;
bool visited[maxn];
int arr[maxn];
int solve(int S, int E){
int balance = 0;
int ret = 0;
memset(visited, false, sizeof visited);
for(int i = S; i <= E; i ++){
if(s[i] == 'C'){
balance ++;
arr[i] = 1;
}
else{
balance --;
arr[i] = -1;
}
if(balance < 0){
balance = 0;
visited[i] = true;
arr[i] = 0;
ret ++;
}
}
int suff_sum = 0;
int mnn = (1 << 30);
for(int i = E; i >= S; i --){
suff_sum += arr[i];
mnn = min(mnn, suff_sum);
}
balance = 0;
///cout << mnn << " " << ret << endl;
return ret + abs(mnn);
for(int i = E; i >= S; i --){
if(s[i] == 'C'){
balance ++;
}
else if(!visited[i] and s[i] == 'T'){
balance --;
}
// cout << balance << " " ;
if(balance < 0){
balance = 0;
ret ++;
}
}
return ret;
}
int main()
{
ios_base::sync_with_stdio(false);
// ifstream cin("in.txt.txt");
cin >> n >> s;
int q;
cin >> q;
for(int i = 0; i < q; i ++){
int a, b;
cin >> a >> b;
a --;
b --;
cout << solve(a, b) << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
55 ms |
892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
55 ms |
892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
55 ms |
892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |