#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "C:\GCC\debug.h"
#else
#define debug(...) void(42)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
vector<int> a(n);
for (int i = 0; i < n; i++) {
if (s[i] == 'C') {
a[i] = +1;
} else {
a[i] = -1;
}
}
auto Ret = [&](int l, int r) {
int balance = 0;
int res = 0;
vector<int> b(n);
for (int i = l; i <= r; i++) {
if (balance + a[i] >= 0) {
balance += a[i];
} else {
res += 1;
b[i] = true;
}
}
balance = 0;
for (int i = r; i >= l; i--) {
if (b[i]) {
continue;
}
if (balance + a[i] >= 0) {
balance += a[i];
} else {
res += 1;
b[i] = true;
}
}
return res;
};
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int l, r;
cin >> l >> r;
--l, --r;
cout << Ret(l, r) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
328 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
328 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
2113 ms |
2232 KB |
Output is correct |
7 |
Execution timed out |
3052 ms |
1724 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
328 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
2113 ms |
2232 KB |
Output is correct |
7 |
Execution timed out |
3052 ms |
1724 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |