题意:
给你一个长度为
的
串 ,你有两种操作:
1.将一个子串翻转,花费
2.将一个子串中的0变成1,1变成0,花费
求你将这个01串变成全是1的串的最少花费。
首先,我们可以将串按照
这划分,例如:
,可以看出只有相邻的
串间进行操作才是有意义的。
将左面的
串与右面的
进行 “交换” 有两种办法:
1.将
同一修改为
.
2.将该串与靠右的一个
串交换(即翻转).
由于题中
是一个确定的值,这就使得我们每次的交换方法一定是相同的。然而,如果一直用第
种方法进行变换,最终必定还要使用一次
操作来将已经连城的一排
, 统一修改为
。即最小花费为:
,
为原序列中
串的数量。
Code:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 300000 + 4;
int nex[maxn];
char str[maxn];
int main()
{
int n, x, y, cnt = 0;
scanf("%d%d%d",&n,&x,&y);
scanf("%s",str + 1);
str[0] = str[1] == '1' ? '0' : '1';
for(int i = 1;i <= n; ++i)
if(str[i] == '0' && str[i] != str[i - 1]) ++cnt;
if(cnt == 0) printf("0");
else cout << (long long)(cnt - 1) * min(x, y) + y;
return 0;
}