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>
#define all(vec) vec.begin(),vec.end()
using namespace std;
using ll=long long;
using P=pair<int,int>;
const ll MOD=1000000007LL;
const ll INF=(1<<30);
const ll LINF=(1LL<<60);
template<typename T> void chmax(T &a,T b){a=max(a,b);}
template<typename T> void chmin(T &a,T b){a=min(a,b);}
struct Max{
int n;
vector<int> dat;
Max(int n_){
n=1;
while(n<n_)n<<=1;
dat.resize(2*n,-INF);
}
void update(int k,int x){
k+=n;
dat[k]=x;
k>>=1;
while(k>0){
dat[k]=max(dat[k<<1],dat[k<<1|1]);
k>>=1;
}
}
int query(int l,int r){
l+=n;r+=n;
int res=-INF;
for(++r;l<r;l>>=1,r>>=1){
if(l&1)chmax(res,dat[l++]);
if(r&1)chmax(res,dat[--r]);
}
return res;
}
};
struct Min{
int n;
vector<int> dat;
Min(int n_){
n=1;
while(n<n_)n<<=1;
dat.resize(2*n,INF);
}
void update(int k,int x){
k+=n;
dat[k]=x;
k>>=1;
while(k>0){
dat[k]=min(dat[k<<1],dat[k<<1|1]);
k>>=1;
}
}
int query(int l,int r){
l+=n;r+=n;
int res=INF;
for(++r;l<r;l>>=1,r>>=1){
if(l&1)chmin(res,dat[l++]);
if(r&1)chmin(res,dat[--r]);
}
return res;
}
};
int main(){
int n;cin>>n;
string s;cin>>s;
vector<int> p;
for(int i=0;i<n;i++){
if(s[i]=='x')p.push_back(i);
}
int nn=p.size();
if(nn>12)return 0;
int ans=0;
for(int b=0;b<(1<<nn);b++){
for(int j=0;j<nn;j++){
if((b>>j)&1){
s[p[j]]='(';
}else{
s[p[j]]=')';
}
}
vector<int> sum(n+10),mi(n+10,INF);
mi[0]=0;
Max seg1(n+10);
Min seg2(n+10);
for(int i=0;i<n;i++){
if(s[i]=='('){
sum[i+1]=sum[i]+1;
}else{
sum[i+1]=sum[i]-1;
}
chmin(mi[i+1],mi[i]);
chmin(mi[i+1],sum[i+1]);
seg1.update(i+1,sum[i+1]);
seg2.update(i+1,sum[i+1]);
}
if(mi[n]==0&&sum[n]==0){
ans++;
continue;
}
if(abs(sum[n])%2)continue;
bool f=false;
for(int l=1;l<=n;l++){
if(mi[l-1]<0)continue;
for(int r=l;r<=n;r++){
if(sum[n]/2-(sum[r]-sum[l-1])==0&&seg1.query(l,r)<=2*sum[l-1]&&seg2.query(r+1,n)-2*(sum[r]-sum[l-1])>=0){
f=true;
ans++;
break;
}
}
if(f)break;
}
}
cout<<ans<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |