B. Saving the City 灵茶 -- 2024-03-25

30 篇文章 0 订阅
14 篇文章 0 订阅

链接 :

 Problem - 1443B - Codeforces

思路 : 

双指针

对于每一段夹在1之间的0,求其长度,如果len*b<=a,那么可以用地雷先铺满这一段路;

代码 : 

// https://codeforces.com/problemset/problem/1443/B
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;

inline void solve(){
	int a , b ; cin >> a >> b ;
	string s ; cin >> s ;
	int n = s.size() ;
	s = ' ' +  s ; 
	LL ans = 0 ;
	int i = 1 ;
	while(i<=n && s[i] == '0') i++ ;
	int j = n ;
	while(j>=1&&s[j]=='0') j-- ;
	if(j<i){
		cout << 0 << endl ;
		return ;
	}
	for(;i<=j;i++){
		if(s[i]=='1') {
			while(i<=j && s[i]=='1') i++ ;
			i--;
			ans += a ;
		}else{
			int r = i ;
			while(r<=j&&s[r]=='0') r++ ;
			int len = r - i ;
			if(len * b <= a){
				ans += len*b-a;
			}
			i = r - 1 ;
		}
	}
	cout << ans << endl ;
}
 
signed main(){
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值