【Codeforces 1443 B】Saving the City,贪心,分类讨论

problem

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 x−1 and x+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≤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≤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 105.

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
Note
In the second test case, if we place a mine under the fourth building and then activate it, then all mines on the field are activated. The cost of such operations is six, b=1 coin for placing a mine and a=5 coins for activating.

solution

/*
题意:
+ 给出长为n(1e5)的01序列,放置1的成本为b,激活1的成本为a,激活会使得当前和相邻两个都变为0,且可以蔓延。
+ 求最小成本把字符串变为全0.
思路:
+ 相同的1会被连续掉,所以等价于一个1。对于两个含1的段合并,代价为max(2a, a+b(l2−r1)),合并后为1
+ 暴力扫一遍,当前的代价为填充b(l2-r1)和上面一起炸还是直接a去炸。
+ 原来我也能写出这么sb的代码,,比赛的时候随便嘛,怪我洛
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 110;
int main(){
	ios::sync_with_stdio(false);
	int T;  cin>>T;
	while(T--){
		int a, b;  cin>>a>>b;
		string s;  cin>>s;
		if(s.find("1")==string::npos){
			cout<<0<<endl;
			continue;
		}
		string t="0"; t[0]=s[0];
		for(int i = 1; i < s.size(); i++){
			if(s[i]=='1'){
				if(s[i-1]=='1')continue;
				else t += "1";
			}
			if(s[i]=='0')t += "0";
		}
		s = t;
		int ans = a;
		//cout<<s<<"\n";
		for(int i=s.find("1"); i < s.size(); ){
			if(s.find("1",i+1)==string::npos)break;
			int cur = s.find("1",i+1);
			int d = cur-i-1;
			ans += min(a,b*d);
			i = cur;
		}
		cout<<ans<<"\n";
	}
	return 0;
}


上一篇:错误NotImplementedError: Saving the model to HDF5 format requires the model to be a Functional model


下一篇:数据结构 07-图5 Saving James Bond - Hard Version (30 分)