Codeforces Round #493 (Div. 2) C. Convert to Ones 乱搞_构造_好题

题意:
给你一个长度为 n n 01 01 串 ,你有两种操作:

1.将一个子串翻转,花费 X X

2.将一个子串中的0变成1,1变成0,花费 Y Y

求你将这个01串变成全是1的串的最少花费。

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

猜你喜欢

转载自blog.csdn.net/liyong1009s/article/details/82961768