Saving the City

链接 : http://codeforces.com/problemset/problem/1443/B

标签 : dp greeedy sorting math *1300

思路 :

dp, 应该存在2种划分 :

  • 接着前面的1, 不需要开销.

  • 与前面的1有断开时 :

    • 自已爆破.
    • 填补, 接上前面的1.
AC代码
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi firsqt
#define se second
#define inf 0x3f3f3f3f
const int N = 1e5 + 7;
int a[N];
int f[N];

int main() {	
	IO;
	int _;
	cin >> _;
	while (_--) {
		memset(f, 0, sizeof f);
		int x, y, n = 0;
		cin >> x >> y;
		string s;
		cin >> s;
		for (int i = 0; i < s.size(); ++i) 
			if (s[i] == '1') a[++n] = i;

                f[1] = x;
		for (int i = 2; i <= n; ++i) {
		    if (a[i] == a[i - 1] + 1) f[i] = f[i - 1];
		    else {
		        int t = a[i] - a[i - 1] - 1;
		        f[i] = min(f[i - 1] + x, f[i - 1] + t * y);
		    }
		}
		cout << f[n] << endl;	
		
	}
	return 0;
}

posted @ 2020-11-04 22:51  phr2000  阅读(88)  评论(0编辑  收藏  举报