#include <iostream>
#include <vector>
using namespace std;
using vi = vector<int>;
const int inf = 1'000'000'000;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
vi A(1+N);
for(int i = 1; i <= N; i++)
{
char c;
cin >> c;
if(c == 'T')
A[i] = +1;
else
A[i] = -1;
}
vi p(1+N);
p[0] = 0;
for(int i = 1; i <= N; i++)
p[i] = p[i-1] + A[i];
vi s(1+N);
s[N] = 0;
for(int i = N-1; i >= 0; i--)
s[i] = s[i+1] + A[i+1];
int Q;
cin >> Q;
for(int q = 1; q <= Q; q++)
{
int l, r;
cin >> l >> r;
int pmx = -inf;
for(int i = l-1; i <= r; i++)
{
pmx = max(pmx, p[i]);
}
int c1 = pmx - p[l-1];
// cerr << "c1 = " << c1 << '\n';
// cerr << "pmx = " << pmx << '\n';
// int c2 = 0;
int res = c1;
int currpmax = -inf;
for(int i = l-1; i <= r; i++)
{
currpmax = max(currpmax, p[i]);
// cerr << i << " : " << s[i] - s[r] << " " << currpmax << '\n';
// cerr << "maximum occ = " << currpmax - pmx << '\n';
// c2 = min(c2, max(0, s[i] - s[r]) + currpmax - pmx);
if(s[i] > s[r])
{
// cerr << "! " << s[i]-s[r] << ' ' << (pmx - currpmax) << '\n';
res = max(res, (s[i] - s[r]) + currpmax - p[l-1]);
}
}
cout << res << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
344 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
344 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Execution timed out |
3094 ms |
1356 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
344 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Execution timed out |
3094 ms |
1356 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |