CodeForces - 998C - Convert to Ones(思维)

题目描述:

题目传送门:http://codeforces.com/problemset/problem/998/C
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.

You 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
inputCopy
5 1 10
01000
outputCopy
11
inputCopy
5 10 1
01000
outputCopy
2
inputCopy
7 2 3
1111111
outputCopy
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.

题目大意:

给定一个指定长度的均由0,1组成的字符串,有两种操作可以进行。第一种操作是将一个字串倒序,花费为X;第二种操作是将一个字串取反(0->1,1->0),花费为Y。问最少要花费多少钱,才能将所有的字符串都变成1。

题目分析:(来自于「Bug_Programmer」)

任何一段0 1序列都可以看做是一串 0 然后用 1 切割开。首先,因为我们是要把目标串变成全1串,所以开头的1(和结尾的1)我们可以不去管它,所以我们可以把所有的串看做是这种(开头的1和结尾的1无所谓就全都省略)然后我们可以怎么做呢?例如:000 1000 10000 100 100这个串,因为上面操作的花费与段的长度无关,所以我们可以把相邻的1合成一个1,相邻的0合成一个0。所以原串就可以转化成 0 10 10 10 10。

假设0分成的段的数量是num。

第一种方法,我们可以选择第二段 10 ,对其进行倒置操作,所以整个串就变成了 0 01 10 10 10。然后再次合并相邻的1 和相邻的 0 ,原串变成了 0 10 10 10。然后在进行一次相同的操作,就变成了 0 10 10.以此类推,最后将变成 0 10,再倒置一次,变成 00 1,然后再对00 进行一次操作二即可。这种方法的花费为 (num-1)*x+y。

第二种方法,我们直接对每一段0实施操作二,使得其变为全1串,这样的花费是num*y。

所以显然,当x<=y时,我们选择第一种方案,当x>y时,我们选择第二中方案,统计0的段数,直接输出结果即可。

可以加一个特判,如果数列全部为1,则直接输出0。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
char s[300005];
int main()
{
    long long n,x,y,ans=0;
    cin>>n>>x>>y;
    scanf("%s",&s); 
    int flag=0;
    int cut=0;
    for(int i=0;i<n;i++)
    if(s[i]=='0')
    flag=1;
    if(flag==0)
    {
    	cout<<0<<endl;
    	return 0;
	}
    if(s[0]=='0')
    cut++;
    for(int i=1;i<=n;i++)
    if(s[i]=='0'&&s[i-1]=='1')
    cut++;
    if(x<=y)
    {
    	ans=x*(cut-1)+y;
	}
	if(x>y)
	{
		ans=y*cut;
	}
	cout<<ans<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值