B. Saving the City

题目传送门

B. Saving the City

题目大意

给你一个01字符串s,消除其中连续的1耗费a,将一个0变为1耗费b,求将整个字符串s全部变成0所消耗的最小值

思路

取将不相连的两段相连与否的最小值即可,即为
第一个连续的1直接耗费a,后面记录到下一段的1的距离(即为中间0的个数),然后 m i n ( s u m ∗ b , a ) min(sum*b,a) min(sumb,a)即可

AC Code

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<math.h>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
// #define int long long
#define debug(a) cout<<#a<<"="<<a<<endl;
typedef long long ll;
const double PI=acos(-1.0);
const double e=exp(1.0);
const int M=1e9+7;
const int N=2e5+7;
inline int mymax(int x,int y){return x>y?x:y;}
inline int mymin(int x,int y){return x<y?x:y;}
int a, b, ans;
string s;
void solve(){
    cin>>a>>b;
    cin>>s;
    ans=0;
    int flag=0, sum=0;
    for(int i=0; i<s.length(); i++){
        if(s[i]=='1'){
            if(!flag) flag=1, ans=a;
            else      ans+=min(a,sum*b);
            sum=0;
        }
        if(s[i]=='0') sum++;
    }
    cout<<ans<<endl;
	return ;
}

signed main(){
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值