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串,这样的花费是numy。
所以显然,当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=cuty;
if(cut!=0)
printf("%lld",ans);
else
printf("0");
return 0;
} `
Convert to Ones的更多相关文章
- Convert BSpline Curve to Arc Spline in OpenCASCADE
Convert BSpline Curve to Arc Spline in OpenCASCADE eryar@163.com Abstract. The paper based on OpenCA ...
- Convert.ToInt32()、int.Parse()和(int)三者的区别
Convert.ToInt32将object类类型转换成int类型,如Convert.ToInt32(session["shuzi"]); (int)适合简单数据类型之间的转换: ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
- 5 Convert Sorted List to Binary Search Tree_Leetcode
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- Unable to convert MySQL date/time value to System.DateTime 错误
C#读取MySql时,如果存在字段类型为date/datetime时的可能会出现以下问题“Unable to convert MySQL date/time value to System.DateT ...
- SQL Server CONVERT() 截取日期
SELECT CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSELECT CONVERT(varchar(100), GETDATE() ...
- tomcat启动时候报错Can't convert argument: null
一.启动报错: 为了避免导入的项目重名,我先修改了前一个项目的名称. 重新启动该项目至tomcat,报错:java.lang.IllegalArgumentException: Cant conver ...
随机推荐
- JAVA实现对称加密
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.对称加密算法DES 1.概述:采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密,这 ...
- ASP.NET给图片自动添加水印
先建一个类,感觉注释已经很详细了,有不懂的欢迎评论 using System; using System.Collections.Generic; using System.Drawing; usin ...
- Java实现Fibonacci取余
Description Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. Input 多 ...
- java实现第三届蓝桥杯方块填数
方块填数 "数独"是当下炙手可热的智力游戏.一般认为它的起源是"拉丁方块",是大数学家欧拉于1783年发明的. 如图[1.jpg]所示:6x6的小格被分为6个部 ...
- 案例:DG主库未设置force logging导致备库坏块
DG搭建时,官方文档手册有明确提到要设置数据库为force_logging,防止有nologging操作日志记录不全导致备库应用时出现问题. 虽然是老生常谈的安装规范,但现实中总会遇到不遵守规范的场景 ...
- 聊一聊Asp.net过滤器Filter那一些事
最近在整理优化.net代码时,发现几个很不友好的处理现象:登录判断.权限认证.日志记录.异常处理等通用操作,在项目中的action中到处都是.在代码优化上,这一点是很重要着力点.这是.net中的过滤器 ...
- [蓝桥杯]算法提高 GPA
问题描述 输入A,B两人的学分获取情况,输出两人GPA之差. 输入格式 输入的第一行包含一个整数n表示A的课程数,以下n行每行Si,Ci分别表示第i个课程的学分与A的表现. GPA=Σ(Si*Ci) ...
- public、potected 、private继承下的子类对父类成员的访问情况
#include<iostream> #include<string> using namespace std; class parent{ protected: int m_ ...
- 总结:PgSql备份pg_dump与还原pg_restore
备份还原方法:pg_dump和pg_restore,先仔细说明这两个命令,再记录我的操作方法. 远程复制scp: #which scp /usr/bin/scp #rpm -qf /usr/bin/ ...
- is ==小数据池编码解码
== 比较 比较的是两边的值 is 比较 比较的是内存地址 判断两个东西指向的是不是同一个对象 取内存地址 id() 小数据池 ...