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实现字符串匹配

    1 问题描述 给定一个n个字符组成的串(称为文本),一个m(m <= n)的串(称为模式),从文本中寻找匹配模式的子串. 2 解决方案 2.1 蛮力法 package com.liuzhen.c ...

  2. Java实现 洛谷 P2141 珠心算测验

    import java.util.LinkedList; import java.util.Scanner; public class Main { private static Scanner ci ...

  3. 【CSS】电脑、移动端公用样式

    电脑端: /* Public */ @charset "utf-8"; html, body, div, p, ul, ol, li, dl, dt, dd, h1, h2, h3 ...

  4. (前言一)HTTP报文

    01 概述 客户端与服务器端之间的通信,通过HTTP协议,以HTTP报文的形式来实现数据的交互. HTTP报文是HTTP通信时发送的数据块,本文主要从以下几个方面介绍HTTP报文:HTTP报文结构.方 ...

  5. Java Review (一、Java开发环境)

    @ 目录 Java程序运行机制 高级语言运行机制 编译型语言 解释型语言 Java运行机制和JVM 编写 编译 运行 Java开发工具包 JDK JRE JDK.JRE与JVM HelloWord 编 ...

  6. 一文了解Docker容器技术的操作

    一文了解Docker容器技术的操作 前言一.Docker是什么二.Docker的安装及测试Docker的安装Docker的Hello world测试三.Docker的常见操作镜像的基本操作容器的基本操 ...

  7. 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'xxxx'中的标识列指定显式值

    执行以下sql INSERT INTO [Country] VALUES (, N'中国', N'China', N'CN'); 提示错误 仅当使用了列列表并且 IDENTITY_INSERT 为 O ...

  8. 滴滴数据驱动利器:AB实验之分组提效

    桔妹导读:在各大互联网公司都提倡数据驱动的今天,AB实验是我们进行决策分析的一个重要利器.一次实验过程会包含多个环节,今天主要给大家分享滴滴实验平台在分组环节推出的一种提升分组均匀性的新方法.本文首先 ...

  9. pytorch入门2.1构建回归模型初体验(模型构建)

    pytorch入门2.x构建回归模型系列: pytorch入门2.0构建回归模型初体验(数据生成) pytorch入门2.1构建回归模型初体验(模型构建) pytorch入门2.2构建回归模型初体验( ...

  10. zabbix服务的部署

    1.zabbix的介绍 zabbix是一个基于WEB界面分布式系统监视以及网络监视功能的企业的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并且提供灵活的通知机制以让系统管 ...