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实现 LeetCode 429 N叉树的层序遍历
429. N叉树的层序遍历 给定一个 N 叉树,返回其节点值的层序遍历. (即从左到右,逐层遍历). 例如,给定一个 3叉树 : 返回其层序遍历: [ [1], [3,2,4], [5,6] ] 说明 ...
- java实现第五届蓝桥杯格子放鸡蛋
格子放鸡蛋 X星球的母鸡很聪明.它们把蛋直接下在一个 N * N 的格子中,每个格子只能容纳一枚鸡蛋.它们有个习惯,要求:每行,每列,以及每个斜线上都不能有超过2个鸡蛋.如果要满足这些要求,母鸡最多能 ...
- 关于晶体问题TCXO_14.7456MHZ
如何判断热点的晶体好不好,首先,看偏移,偏移为0的晶体一般就是温补晶体,当然偏移是500或者几百固定的也是温补,但是不是我们首选的温补晶体 因为偏移为0非常省事,这是系统默认的偏移0,因此设置好频率就 ...
- install-package : 由于无法加载项目 mydemo 的详细信息,操作失败
安装nuget package包提示错误 install-package : 由于无法加载项目 mydemo 的详细信息,操作失败 解决方法 项目用dotnet cli 命令dotnet new mv ...
- [原创][开源] SunnyUI.Net 主题
SunnyUI.Net, 基于 C# .Net WinForm 开源控件库.工具类库.扩展类库.多页面开发框架 Blog: https://www.cnblogs.com/yhuse Gitee: h ...
- throws,throw,try,catch,finally 分别代表什么 意义?
Java通过面向对象的方法进行异常处理,把各种不同的异常进行分类,并提供了良好的接口. 在Java中,每个异常都是一个对象,它是Throwable类或其它子类的实例.当一个方法出现异常后便 抛出一个异 ...
- .net core3.1 abp动态菜单和动态权限(思路) (二)
ps:本文需要先把abp的源码下载一份来下,跟着一起找实现,更容易懂 在abp中,对于权限和菜单使用静态来管理,菜单的加载是在登陆页面的地方(具体是怎么知道的,浏览器按F12,然后去sources中去 ...
- CSS中的百分比(%)如何使用???
除了height以外垂直方向上的margin-top(bottom)或者padding-top(bottom)的百分比取值都是相对于父元素的宽度 在默认的content-box盒模型下元素的width ...
- 学习nginx从入门到实践(五) 场景实践之静态资源web服务
一.静态资源web服务 1.1 静态资源 静态资源定义:非服务器动态生成的文件. 1.2 静态资源服务场景-CDN 1.3 文件读取配置 1.3.1 sendfile 配置语法: syntax: se ...
- 线上排查Class、Jar加载问题的一般方法
问题背景 本问题源于<ojdbc6中OraclePreparedStatement的ArrayIndexOutOfBoundsException异常BUG-6396242>这篇博文中最后思 ...