SRM 688 ParenthesesDiv2Hard

给出一段仅由左右括号组成的字符串,和若干个区间(L,R) (各个区间不重叠),问要使每个区间内的序列都合法,最少需要进行多少次交换(每次交换可以互换任意两个位置上的字符)。

思路

<1>对于每一个的区间一定要满足的是: ( 个数始终大于)于是每次cnt[1]>cnt[0]时就使cnt[1]–,cnt[0]++,res++ 表示把)变成(的个数 res为A;
<2>最终造成 ( 多一些于是需要把(cnt[1]-cnt[0])/2个 ( 换成 ) 为B;
<3>如果A比B大就需要额外的)反之需要(

const int M=10005;
class ParenthesesDiv2Hard {
public:
    bool G[M],use[M];int need[2],sum[2];
    int minSwaps(string s, vector <int> L, vector <int> R) {
        int n=s.size(),m=L.size(),ans=0;
        for(int i=0;i<n;i++)G[i]=(s[i]==')');
        for(int i=0;i<m;i++)
            for(int j=L[i];j<=R[i];j++)use[j]=1;
        for(int i=0;i<n;i++)if(!use[i])sum[G[i]]++;
        for(int i=0;i<m;i++){
            if((R[i]-L[i]+1)%2)return -1;
            int sum=0,res=0;
            //res:how many ')' should be change into '('
            //sum:the sum of rest '('
            for(int j=L[i];j<=R[i];j++){
                if(!G[j])sum++;
                else sum--;
                if(sum<0)sum+=2,res++;
            }
            sum/=2;
            //sum:'(' was rest and should be exchanged sum/2 times
            //->sum/2:how many '(' should be change into ')'
            ans+=min(sum,res);
            if(res>sum)need[0]+=res-sum;
            else need[1]+=sum-res;
        }
        int chang_inside=min(need[0],need[1]);
        //min(need[0],need[1]):how many '(',')'can be fixed inside 
        if(need[0]-chang_inside>sum[0]||need[1]-chang_inside>sum[1])return -1;
        ans+=max(need[0],need[1]);
        //ans+=min(need[0],need[1])+max(need[0],need[1])-min(need[0],need[1]);
        //     which be exchanged inside  ||   the rest '('or ')' also needs one time to fix;
        return ans;
    }
};
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值