QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#621382#3420. Jingle BallswangqingxianWA 0ms7684kbC++202.2kb2024-10-08 14:05:502024-10-08 14:05:51

Judging History

你现在查看的是最新测评结果

  • [2024-10-08 14:05:51]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:7684kb
  • [2024-10-08 14:05:50]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;
const int N=3e5+10,inf=1e9,mod=1e4+7;
vector<int>dp[N];
int genshin;
int cnt=0;
int ls[N],rs[N],stk[N],top,tot=0,sum[N];
bool lf[N];
void build(string s){
    for(int i=1;i<=cnt;++i){
        dp[i].clear();
        ls[i]=rs[i]=0;
        sum[i]=0;lf[i]=0;
        stk[i]=0;
    }
    top=0;tot=0;cnt=0;
    for(int i=0;i<s.size();++i){
        if(s[i]=='(')stk[++top]=++cnt;
        else if(s[i]=='B'){
            lf[cnt]=1;
            ++tot;
        }
        else{
            --top;
            if(ls[stk[top]])rs[stk[top]]=stk[top+1];
            else ls[stk[top]]=stk[top+1];
        }
    }
    // dp[0].clear();
    // dp[0].resize(tot);
    // for(int i=0;i<=tot;++i)dp[0][i]=inf;
}
void dfs(int t,int siz){
    if(!t)return;
    dp[t].resize(siz+1);
    for(int i=0;i<=siz;++i)dp[t][i]=inf;
    if(!ls[t]&&!rs[t]){
        dp[t][lf[t]]=0;
        dp[t][1-lf[t]]=1;
        sum[t]=1;
        return;
    }
    dfs(ls[t],siz/2+1);
    if(!rs[t]){
        dp[t][1]=dp[ls[t]][1];
        dp[t][0]=dp[ls[t]][0];
        return;
    }
    dfs(rs[t],siz/2+1);
    sum[t]=sum[ls[t]]+sum[rs[t]];
    for(int i=0;i<=min(siz,sum[t]);++i){
        if(dp[ls[t]].size()<=i/2||dp[rs[t]].size()<=i/2)continue;
        if(!(i&1))
            dp[t][i]=min(dp[t][i],dp[ls[t]][i/2]+dp[rs[t]][i/2]);
        else{
            if(dp[rs[t]].size()>i/2+1)
                dp[t][i]=min(dp[t][i],dp[ls[t]][i/2]+dp[rs[t]][i/2+1]);
            if(dp[ls[t]].size()>i/2+1)
                dp[t][i]=min(dp[t][i],dp[ls[t]][i/2+1]+dp[rs[t]][i/2]);
        }
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    string s;
    while(cin>>s){
        cin>>s;
        build(s);
        dfs(1,tot);
        // for(int i=1;i<=cnt;++i){
        //     cout<<i<<endl;
        //     for(int j=0;j<dp[i].size();++j)cout<<dp[i][j]<<" ";
        //     cout<<endl;
        // }
        if(dp[1][tot]!=inf)cout<<dp[1][tot]/2<<endl;
        else cout<<"impossible"<<endl;
    }
    // // cerr<<fixed<<setprecision(3)<<(double)clock()/CLOCKS_PER_SEC<<endl;
    return 0;
}
/*
 */

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 7684kb

input:

((B)())
((((B)(B))((B)()))(B))
(()(((B)(B))(B)))

output:

impossible
1

result:

wrong answer 1st lines differ - expected: '0', found: 'impossible'