B. Saving the City(贪心)

题目

题目描述

B. Saving the City
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Bertown is a city with 𝑛n buildings in a straight line.

The city’s security service discovered that some buildings were mined. A map was compiled, which is a string of length 𝑛n, where the 𝑖i-th character is “1” if there is a mine under the building number 𝑖i and “0” otherwise.

Bertown’s best sapper knows how to activate mines so that the buildings above them are not damaged. When a mine under the building numbered 𝑥x is activated, it explodes and activates two adjacent mines under the buildings numbered 𝑥−1x−1 and 𝑥+1x+1 (if there were no mines under the building, then nothing happens). Thus, it is enough to activate any one mine on a continuous segment of mines to activate all the mines of this segment. For manual activation of one mine, the sapper takes 𝑎a coins. He can repeat this operation as many times as you want.

Also, a sapper can place a mine under a building if it wasn’t there. For such an operation, he takes 𝑏b coins. He can also repeat this operation as many times as you want.

The sapper can carry out operations in any order.

You want to blow up all the mines in the city to make it safe. Find the minimum number of coins that the sapper will have to pay so that after his actions there are no mines left in the city.

Input
The first line contains one positive integer 𝑡t (1≤𝑡≤1051≤t≤105) — the number of test cases. Then 𝑡t test cases follow.

Each test case begins with a line containing two integers 𝑎a and 𝑏b (1≤𝑎,𝑏≤10001≤a,b≤1000) — the cost of activating and placing one mine, respectively.

The next line contains a map of mines in the city — a string consisting of zeros and ones.

The sum of the string lengths for all test cases does not exceed 105105.

Output
For each test case, output one integer — the minimum number of coins that the sapper will have to pay.

Example
inputCopy
2
1 1
01000010
5 1
01101110
outputCopy
2
6

题目大意

就是给你一串“01"字符串,然后给你两个数a和b,‘1’代表炸弹,'0‘代表啥也没有,引爆一个炸弹需要花费金额为a,当你引爆一个炸弹的时候,与该炸弹相邻的炸弹都会被引爆,你也可以花费金额b来在’0‘的位置埋一个炸弹,问你引爆炸弹花费最小的金额。

分析

解题思路

首先第一个炸弹之前的’0‘都不用管,后面的就判断两个1之间的0是埋炸弹花费少还是直接点燃花费少。

AC代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    int t,a,b;
    string s;
    cin>>t;
    while(t--)
    {
        ll sum=0;
        int cnt=0,flag=0;
        cin>>a>>b;
        cin>>s;
        int n=s.size();
        for(int i=0; i<n; i++)
        {
            if(!flag)
            {
                if(s[i]=='1')
                {
                    sum+=a;
                    flag++;
                }
            }
            else
            {
                if(s[i]=='0')
                    cnt++;
                else
                {
                    if(s[i-1]=='0')
                        sum+=min(a,b*cnt);
                    cnt=0;
                }
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值