Codeforces998C——Convert to Ones

You’ve got a string a1,a2,…,an, consisting of zeros and ones.
Let’s call a sequence of consecutive elements ai,ai + 1,…, aj (1≤ i≤ j≤ n) a substring of string a.
ou can apply the following operations any number of times:
Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»);
Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»).
You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring.
What is the minimum number of coins you need to spend to get a string consisting only of ones?
Input
The first line of input contains integers n, x and y (1 ≤ n ≤ 300000,0≤x,y≤109) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring).
The second line contains the string a of length n, consisting of zeros and ones.
Output
Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations.
Examples
Input
5 1 10
01000
Output
11
Input
5 10 1
01000
Output
2
Input
7 2 3
1111111
Output
0
Note
In the first sample, at first you need to reverse substring [1…2], and then you need to invert substring [2…5].
Then the string was changed as follows:
«01000» → «10000» → «11111».
The total cost of operations is 1+10=11.
In the second sample, at first you need to invert substring [1…1], and then you need to invert substring [3…5].
Then the string was changed as follows:
«01000» → «11000» → «11111».
The overall cost is 1+1=2.
In the third example, string already consists only of ones, so the answer is 0.

很牛比的一道题 首先花费的最小肯定和x和y的大小关系有关 所以这应该是两种策略
比如x小 就优先考虑反转 再取反 y小就相反
然后这题的巧妙之处还在于一段0和一个0其实是一样的 ,所以要把字符串压缩一下,算出0的个数即可(头和尾的1都不用算) 也就是最后得到的字符串是0101….010这样子
然后看0的个数和xy的关系即可

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
int n;
long long x,y;
string s;
int main(void){
    scanf("%d%d%d",&n,&x,&y);
    cin >> s;
    int l=s.size();
    int i=0;
    while(i<l && s[i]=='1'){
        i++;
    }
    long long cnt=(i==l) ? 0: 1;
    int now=s[i];
    for(;i<l;i++){
        while(s[i]==now){
            i++;
        }
        now=s[i];
        if(now=='0'){
            cnt++;
        }
    }
    //压缩后0的个数
    //printf("%d\n",cnt);
    long long ans=0;
    if(cnt==0){
        ans=0;
    }
    else if(x<y){
        ans=(cnt-1)*x+y;
    }
    else{
        ans=cnt*y;
    }
    printf("%lld\n",ans);
    return 0;
}
全部评论

相关推荐

rbjjj:太杂了吧,同学,项目似乎都没深度,都是api调度耶,分层架构思想没有体现出来了,前端没有前端优化前端工程化体现,后端微服务以及分层架构没体现以及数据安全也没体现,核心再改改,注重于计算机网络,工程化,底层原理吧
点赞 评论 收藏
分享
dian3b:挺妙的,如果上纲上线显得不合人心,但是这样以来既能监督适当摸鱼,也有一定的人文关怀。
摸鱼被leader发现了...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务