zoukankan      html  css  js  c++  java
  • Convert to Ones

    题目

    You've got a string $ a_1,a_2,…,a_n $ , consisting of zeros and ones.

    Let's call a sequence of consecutive elements $ a_i,a_i + 1,…, a_j $ (1≤i≤j≤n) a substring of string aa.

    You can apply the following operations any number of times:

    • Choose some substring of string aa (for example, you can choose entire string) and reverse it, paying xx coins for it (for example, «0101101» →→ «0111001»);

    • Choose some substring of string aa (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 yy 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?

    输入格式

    The first line of input contains integers n, x and y $ (1≤n≤300000,0≤x,y≤10^9) $ — 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 aa of length n, consisting of zeros and ones.

    输出格式

    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.

    样例

    Input

    5 1 1001000
    

    Output

    11
    

    Input

    5 10 101000
    

    Output

    2
    

    Input

    7 2 31111111
    

    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=111+10=114).

    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=21+1=2).

    In the third example, string already consists only of ones, so the answer is 0.

    题意

    给定一串0-1序列,定义两种操作:

    • 操作一:选取一连续子串倒置。
    • 操作二:选取一连续子串把进行01互换(取反)。
    • 并给出操作一和操作二的代价,分别为x和y。
      操作到最后要把串变成只含1的串,问最小的操作代价。

    题解

    那么这道题何解呢?

    其实这道题不是很难,看懂解析后,代码便轻而易举。

    这道题要把所有连续的0换成1,那么最笨的就是挨个替换,如串00100100,如果不进行操作一,我们需要三次操作,那么如果进行操作一呢,如将第三位的1和第五位的0替换一次,那么显然现在还需要进行两次操作二,总共加起来还是三次操作。这样我们发现操作总次数是不会变的, 那么至此,已经可以枚举出答案了。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 300000+10;;
    long long n,x,y,cnt;
    char ch[maxn];
    int main(){
        ch[0]='1';
    	scanf("%lld%lld%lld%s",&n,&x,&y,ch+1);
    	for(int i=1;i<=n;++i)
                if(ch[i]=='0'&&ch[i-1]=='1') cnt++;
    	if(cnt == 0)
                printf("0
    ");
    	else 
                printf("%lld
    ",(cnt-1)*min(x,y)+y);
    	return 0;
    }
    
  • 相关阅读:
    Aix_bugzilla
    aix Mysql安装 Oracle官方教程
    Aix6.1安装openssh
    日媒:阿里巴巴上市融资或超Facebook
    设计模式(一)---单例模式
    Handler具体解释系列(七)——Activity.runOnUiThread()方法具体解释
    TCP/IP协议族——IP工作原理及实例具体解释(上)
    leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法
    Material Design之RecyclerView的使用(一)
    jQuery和CSS3超酷表单美化插件
  • 原文地址:https://www.cnblogs.com/hellohhy/p/12686589.html
Copyright © 2011-2022 走看看