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;

} `

Convert to Ones的更多相关文章

  1. 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 ...

  2. Convert.ToInt32()、int.Parse()和(int)三者的区别

    Convert.ToInt32将object类类型转换成int类型,如Convert.ToInt32(session["shuzi"]); (int)适合简单数据类型之间的转换: ...

  3. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  4. [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制

    Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...

  5. [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 ...

  6. [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. 这道 ...

  7. 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 ...

  8. Unable to convert MySQL date/time value to System.DateTime 错误

    C#读取MySql时,如果存在字段类型为date/datetime时的可能会出现以下问题“Unable to convert MySQL date/time value to System.DateT ...

  9. SQL Server CONVERT() 截取日期

    SELECT CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSELECT CONVERT(varchar(100), GETDATE() ...

  10. tomcat启动时候报错Can't convert argument: null

    一.启动报错: 为了避免导入的项目重名,我先修改了前一个项目的名称. 重新启动该项目至tomcat,报错:java.lang.IllegalArgumentException: Cant conver ...

随机推荐

  1. Java实现 LeetCode 786 第 K 个最小的素数分数(大小堆)

    786. 第 K 个最小的素数分数 一个已排序好的表 A,其包含 1 和其他一些素数. 当列表中的每一个 p<q 时,我们可以构造一个分数 p/q . 那么第 k 个最小的分数是多少呢? 以整数 ...

  2. (Java实现) 洛谷 P1031 均分纸牌

    题目描述 有NN堆纸牌,编号分别为 1,2,-,N1,2,-,N.每堆上有若干张,但纸牌总数必为NN的倍数.可以在任一堆上取若干张纸牌,然后移动. 移牌规则为:在编号为11堆上取的纸牌,只能移到编号为 ...

  3. (Java实现) 洛谷 P1091合唱队形

    题目描述 NN位同学站成一排,音乐老师要请其中的(N−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,-,K1,2,-,K,他们的身 ...

  4. Java实现 LeetCode 657 机器人能否返回原点(暴力大法)

    657. 机器人能否返回原点 在二维平面上,有一个机器人从原点 (0, 0) 开始.给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束. 移动顺序由字符串表示.字符 move[i ...

  5. Java实现 蓝桥杯VIP 基础练习 分解质因数

    题目介绍 问题描述 求出区间[a,b]中所有整数的质因数分解. 输入格式 输入两个整数a,b. 输出格式 每行输出一个数的分解,形如k=a1a2a3-(a1<=a2<=a3-,k也是从小到 ...

  6. Java实现 蓝桥杯VIP 算法训练 筛选号码

    算法训练 筛选号码 Description 有n个人围成一圈,顺序排号(编号为1到n).从第1个人开始报数(从1到3报数),凡报到3的人退出圈子.从下一个人开始继续报数,直到剩下最后一个人,游戏结束. ...

  7. Java中环境变量PATH与CLASSPATH的区别

    在安装JDK时需要添加环境变量,经常使用的环境变量有两个: PATH与CLASSPATH 下面总结一下环境变量的作用. PATH是系统用来指定可执行文件的完整路径.当在CMD中执行命令时,如果执行的可 ...

  8. Spring Data Jpa Specification 调用Oracle 函数/方法

    开发框架用的Jpa,数据库是 Oracle. 在开发中难免会遇到需要数据库字段是字符串格式,但是又需要对其进行范围查询(数据库设计问题,后续应避免).那么问题来了, Jpa Specification ...

  9. Linux文件搜索命令locate、which、grep详解

    命令locate详解 命令locate,其基本功能是在文件资料库中可以快速的搜索系统文件,占用系统资源很少,例如:locate my.cnf 还可以使用locate -i [文件名],不区分大小写进行 ...

  10. inotify监听文件

    inotify监听文件并通知 static int inotify_dbfile(const char *spFromRule, const char *spDevFile) { int inotif ...