CodeForces 1443B Saving the City

2片雷区要么引爆2次,要么埋雷连起来引爆一次,所以如果埋雷连起来的花费比引爆一次的花费少就埋雷。

#include<iostream>
#include<algorithm>
using namespace std;

int t;
int a, b;
char s[100010];
vector<int> d;  //tot表示的区域的长度 

int main()
{
	scanf("%d", &t);
	while(t--)
	{
		while(!d.empty()) d.pop_back();
		scanf("%d %d", &a, &b);
		scanf(" %s", s);
		//计算2段有雷区域之间的距离 
		int i = 0, tmp = 0, tot = 0, res = 0;   //tot:2段有地雷区域的中间那段区域的个数 
		while(s[i] == '0') i++;
		if(s[i] == 0) 
		{
			cout << 0 << endl;
			continue;
		}
		for(; s[i] != 0; i++)
		{
			if(s[i] == '0') tmp++;
			else if(s[i] == '1' && tmp != 0)
			{
				d.push_back(tmp);
				tmp = 0;
				tot++;
			} 
		}
		// 
		sort(d.begin(), d.end());
		i = 0;
		while(i < d.size())
		{
			if(d[i]*b < a) 
			{
				tot--;
				res += d[i]*b;
			}
			else break;
			i++;
		}
		res += (tot+1)*a;
		cout << res << endl;
	}
	return 0;
}
上一篇:AngularDart初探


下一篇:Redis主从同步的问题----Failed opening the RDB file dump.rdb for saving: Permission denied