D2 - Zero-One (Hard Version)(线性dp)

D2 - Zero-One (Hard Version)(线性dp)

传送门

在这里插入图片描述

样例输入:

6
5 8 9
01001
00101
6 2 11
000001
100000
5 7 2
01000
11011
7 8 3
0111001
0100001
6 3 4
010001
101000
5 10 1
01100
01100

样例输出:

8
10
-1
6
7
0

代码:

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

typedef long long LL;
const int N = 5005;
int n;
double x, y, f[N];
string a, b;
int s[N], m;

int main()
{
	int T;
	cin >> T;
	while(T -- )
	{
		m = 0;
		cin >> n >> x >> y >> a >> b;
		for(int i = 0; i < n; i ++ )
			if(a[i] ^ b[i])
				s[ ++ m ] = i;
		
		x = min(x, y * 2);	
		y /= 2;
		f[1] = y;
		if(m % 2) cout << -1 << endl;
		else if(m == 2 && s[2] - s[1] == 1) printf("%d\n", (int)x);
		else
		{
			for(int i = 2; i <= m; i ++ )
				f[i]  = min(f[i - 2] + 1.0 * (s[i] - s[i - 1]) * x, f[i - 1] + y);
				
			printf("%lld\n",(LL)f[m]);
		}
		
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值