#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define vl vector<long long>
#define vii vector<pair<int,int>>
#define vll vector<pair<long long,long long>>
#define pb push_back
#define ll long long
#define ld long double
#define nl '\n'
#define boost ios::sync_with_stdio(false)
#define mp make_pair
#define se second
#define fi first
#define fore(i, y) for(ll i = 0; i < y; i++)
#define forr(i,x,y) for(int i = x;i<=y;i++)
#define forn(i,y,x) for(ll i = y; i >= x; i--)
#define all(v) v.begin(),v.end()
#define sz(v) v.size()
#define clr(v,k) memset(v,k,sizeof(v))
#define rall(v) v.rbegin() , v.rend()
#define pii pair<int,int>
#define pll pair<ll , ll>
const ll MOD = 1e9 + 7;
const ll INF = 1e18 + 1;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return a * (b / gcd(a , b));} // least common multiple (PPCM)
// HERE IS THE SOLUTION
int solve(vi &v)
{
int m = sz(v);
vector<bool> vis(m , 0);
vi pref(m) , suff(m);
pref[0] = v[0];
if(pref[0]<0)vis[0] = 1 , pref[0] = 0;
suff[m - 1] = v[m - 1];
if(suff[m - 1] < 0)vis[m - 1] = 1 , suff[m - 1] = 0;
forr(i , 1 , m - 1)
{
pref[i] = pref[i - 1] + v[i];
if(pref[i] < 0)
{
pref[i] = pref[i - 1];
vis[i] = 1;
}
}
forn(i , m - 2 , 0)
{
suff[i] = suff[i+1] + v[i];
if(suff[i] < 0 || vis[i])
{
suff[i] = suff[i + 1];
vis[i] = 1;
}
}
int ans = 0;
fore(i , m)if(vis[i])ans++;
return ans;
}
int main()
{
cin.tie(0);
cout.tie(0);
boost;
int n , q;
cin>>n;
vi v(n);
fore(i , n)
{
char c;
cin>>c;
v[i] = (c == 'C' ? 1 : -1);
}
cin>>q;
while(q--)
{
int l , r;
cin>>l>>r;
vi a;
forr(i , l - 1 , r - 1)
{
a.pb(v[i]);
}
cout<<solve(a)<<nl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
340 KB |
Output is correct |
2 |
Correct |
9 ms |
432 KB |
Output is correct |
3 |
Correct |
10 ms |
428 KB |
Output is correct |
4 |
Correct |
9 ms |
340 KB |
Output is correct |
5 |
Correct |
8 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
340 KB |
Output is correct |
2 |
Correct |
9 ms |
432 KB |
Output is correct |
3 |
Correct |
10 ms |
428 KB |
Output is correct |
4 |
Correct |
9 ms |
340 KB |
Output is correct |
5 |
Correct |
8 ms |
340 KB |
Output is correct |
6 |
Execution timed out |
3054 ms |
1840 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
340 KB |
Output is correct |
2 |
Correct |
9 ms |
432 KB |
Output is correct |
3 |
Correct |
10 ms |
428 KB |
Output is correct |
4 |
Correct |
9 ms |
340 KB |
Output is correct |
5 |
Correct |
8 ms |
340 KB |
Output is correct |
6 |
Execution timed out |
3054 ms |
1840 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |