C. Convert to Ones Codeforces Round #493 (Div. 2)

C. Convert to Ones
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You've got a string a1,a2,,ana1,a2,…,an, consisting of zeros and ones.

Let's call a sequence of consecutive elements ai,ai+1,,ajai,ai + 1,…, aj (1ijn1≤ 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?

Input

The first line of input contains integers nnxx and yy (1n300000,0x,y1091 ≤ 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 aa of length nn, 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 00, if you do not need to perform any operations.

Examples
input
Copy
5 1 10
01000
output
Copy
11
input
Copy
5 10 1
01000
output
Copy
2
input
Copy
7 2 3
1111111
output
Copy
0
Note

In the first sample, at first you need to reverse substring [12][1…2], and then you need to invert substring [25][2…5].

Then the string was changed as follows:

«01000»  «10000»  «11111».

The total cost of operations is 1+10=111+10=11.

In the second sample, at first you need to invert substring [11][1…1], and then you need to invert substring [35][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 00.

题意:是一段只有0,1的字符串,变成全为1,有两种变法,一种花费x,使任意一段字符串逆转,一种花费是y,使任意一段字符串0变为1,1变0,求最小花费

题解:花费为x的只能改变0,1顺序,只有花费为y的操作能使0改1,但要经量使0连续在一块,要想得到全为1,要么先逆转使0都移到一起再变成1,要么直接让每部分0变1,观察010010可知当有n段字符串为0时(此时是n为3),只需改变n-1(2)次部分字符串就可使0都在一块,知道这个之后只需比较x,y大小即可

代码:如果觉得题解可以欢迎关注评论,如果觉得不好,也欢迎评论提意见,我会加以改进的

#include <bits/stdc++.h>
using namespace std;
int n;
char a[300010];
long long x,y;
int main()
{
	cin>>n>>x>>y;
	cin>>a;
	//cout<<a<<endl; 
	long long k=0;
	for(int i=0;i<n;i++)
	{
		if(a[i]=='0')
		{
			if(i==0)
				k++;
			else
			{
				if(a[i-1]=='1')
					k++;
			}
		}
	}
	//cout<<k<<endl;
	long long re=0;
	if(k==0)
		cout<<0<<endl;
	else
	{
		if(x>=y)
		{
			re = y*k;
		}
		else
		{
			re = x*(k-1)+y;
			
		}
		cout<<re<<endl;
	}
}
阅读更多
换一批

没有更多推荐了,返回首页