This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
#define endl "\n"
#define int ll
using ull=unsigned long long;
using ll=long long;
using pii=pair<int,int>;
const int mod=1e9+7;
#define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n";
template <typename T> istream& operator>>(istream& is, vector<T> &a) {
copy_n(istream_iterator<T>(is), a.size(), a.begin()); return is;}
#ifdef IOI
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
#else
#define dbg(...) 1337;
#endif
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
const int mxn=2e5+100;
signed main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n;
cin>>n;
string s;
cin>>s;
int q;
cin>>q;
vector<int> v(n);
for(int i=0;i<n;i++) v[i]=(s[i]=='C'?1:-1);
auto check = [&](int l,int r,vector<int> &a){
int p=0;
for(int i=l;i<=r;i++){
p+=a[i];
if(p<0) return false;
}
return p>=0;
};
for(int i=0;i<q;i++){
int l,r;
cin>>l>>r;
l--,r--;
int ans=1e18;
for(int j=0;j<=r-l+1;j++){
int x=j;
vector<int> vv=v;
bool ok=true;
int p=0;
for(int i=l;i<=r;i++){
p+=vv[i];
if(p<0) {
if(x==0) ok=false;
p++;
x--;
vv[i]=0;
}
}
if(!ok) continue;
int s=0;
int calc=0;
// dbg(vv)
for(int i=r;i>=l;i--){
s+=vv[i];
// dbg(s,i)
if(s<0){
s++;
calc++;
}
}
// dbg(j,calc)
ans=min(ans,j+calc);
}
cout<<ans<<endl;
}
}
Compilation message (stderr)
election.cpp: In function 'int main()':
election.cpp:38:6: warning: variable 'check' set but not used [-Wunused-but-set-variable]
38 | auto check = [&](int l,int r,vector<int> &a){
| ^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |