#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> s(N);
for (int i = 0; i < N; i++) {
char c;
cin >> c;
if (c == 'C') {
s[i] = 1;
}
else {
s[i] = -1;
}
}
int Q;
cin >> Q;
while (Q-->0) {
int l, r;
cin >> l >> r;
l--, r--;
unordered_set<int> touched;
int cur = 0;
for (int i = l; i <= r; i++) {
cur += s[i];
if (cur < 0) {
cur = 0;
touched.insert(i);
}
}
cur = 0;
for (int i = r; i >= l; i--) {
if (!touched.count(i)) {
cur += s[i];
if (cur < 0) {
cur = 0;
touched.insert(i);
}
}
}
cout << touched.size() << '\n';
}
// for (auto i : touched) {
// cerr << i << " ";
// }
// int ans = (int)(1e9);
// for (int j = 0; j < (1<<N); j++) {
// int cur = 0;
// bool flag = false;
// for (int i = 0; i < N; i++) {
// if (!(j&(1<<i))) cur += s[i];
// if (cur < 0) flag = true;
// }
// cur = 0;
// for (int i = N-1; i >= 0; i--) {
// if (!(j&(1<<i))) cur += s[i];
// if (cur < 0) flag = true;
// }
// if (!flag) {
// ans = min(ans, __builtin_popcount(j));
// }
// }
// if (ans != touched.size()) {
// for (int e : s) {
// cout << e << " ";
// }
// cout << '\n';
// cout << ans << '\n';
// for (int i : touched) {
// cout << i << " ";
// }
// break;
// }
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
364 KB |
Output is correct |
2 |
Correct |
32 ms |
364 KB |
Output is correct |
3 |
Correct |
23 ms |
492 KB |
Output is correct |
4 |
Correct |
48 ms |
364 KB |
Output is correct |
5 |
Correct |
36 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
364 KB |
Output is correct |
2 |
Correct |
32 ms |
364 KB |
Output is correct |
3 |
Correct |
23 ms |
492 KB |
Output is correct |
4 |
Correct |
48 ms |
364 KB |
Output is correct |
5 |
Correct |
36 ms |
364 KB |
Output is correct |
6 |
Execution timed out |
3071 ms |
1076 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
364 KB |
Output is correct |
2 |
Correct |
32 ms |
364 KB |
Output is correct |
3 |
Correct |
23 ms |
492 KB |
Output is correct |
4 |
Correct |
48 ms |
364 KB |
Output is correct |
5 |
Correct |
36 ms |
364 KB |
Output is correct |
6 |
Execution timed out |
3071 ms |
1076 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |