Codeforces Round #493 (Div. 2) C. Convert to Ones 乱搞_构造_好题
题意:
给你一个长度为 n 的 01串 ,你有两种操作:
1.将一个子串翻转,花费 X
2.将一个子串中的0变成1,1变成0,花费 Y
求你将这个01串变成全是1的串的最少花费。
首先,我们可以将串按照0,1这划分,例如:
«00011001110»−>«000»+«11»+«00»+«111»+«0»,可以看出只有相邻的01串间进行操作才是有意义的。
将左面的 0 串与右面的 1 进行 “交换” 有两种办法:
1.将0 同一修改为 1.
2.将该串与靠右的一个1串交换(即翻转).
由于题中X,Y 是一个确定的值,这就使得我们每次的交换方法一定是相同的。然而,如果一直用第 2 种方法进行变换,最终必定还要使用一次 1 操作来将已经连城的一排 0, 统一修改为 1。即最小花费为:(p−1)∗min(x,y)+y,p 为原序列中 0 串的数量。
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;
}