Convert to Ones

Convert to Ones
’You've got a string a 1 , a 2 ,…, a n a1,a2,…,an , consisting of zeros and ones. Let's call a sequence of consecutive elements a i , a i + 1 ,…,  a j ai,ai + 1,…, aj ( 1≤ i≤ j≤ n 1≤ i≤ j≤ n ) a substring of string a a . You can apply the following operations any number of times: Choose some substring of string a a (for example, you can choose entire string) and reverse it, paying x x coins for it (for example, «0101101» → → «0111001»); Choose some substring of string a 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 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?’
汉化大意:
有一个长度为n的只包含0 1 的字符串,现在有两种操作,一种是把这个字符串的某一个连续的子串倒置,花费是x,第二种是,把某一个连续的子串逐位取反(即0变成1 1变成0),花费是y。问你把这个字符串变成全 1 串的最小花费。
思路
任何一段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的段数,直接输出结果即可。
代码
`

include

include

include

include

using namespace std;
const int MAXN=300000+10;
char s[MAXN];
int main(){
long long n,x,y;
scanf("%lld%lld%lld%s",&n,&x,&y,s);
int cut=0;
if(s[0]'0')
cut++;
for(int i=1;i<=n;i++)
if(s[i]
'0'&&s[i-1]=='1')
cut++;
long long ans;
if(x<=y)
ans=(cut-1)x+y;
else
ans=cut
y;
if(cut!=0)
printf("%lld",ans);
else
printf("0");
return 0;
} `

猜你喜欢

转载自www.cnblogs.com/LightyaChoo/p/12717330.html