洛谷 CF997A Convert to Ones

洛谷传送门

题意翻译

给你一个长度为 nn 的01串( n \leq 310^5n*≤3∗105 ),你有两种操作:

1.将一个子串翻转,花费 XX

2.将一个子串中的0变成1,1变成0,花费 YY

求你将这个01串变成全是1的串的最少花费。

感谢@litble 提供翻译

题目描述

You've got a string a_1, a_2, \dots, a_na1,a2,…,a**n , consisting of zeros and ones.

Let's call a sequence of consecutive elements a_i, a_{i + 1}, \ldots, a_j ( 1\leq i\leq j\leq n ) a substring of string aa .

You can apply the following operations any number of times:

  • Choose some substring of string aa (for example, you can choose entire string) and reverse it, paying xx coins for it (for example, «0101101» \to→ «0111001»);
  • Choose some substring of string aa (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 yy coins for it (for example, «0101101» \to→ «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?

输入格式

The first line of input contains integers nn , xx and yy ( 1 \leq n \leq 300,000, 0 \leq x, y \leq 10^9 ) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring).

The second line contains the string aa of length nn , consisting of zeros and ones.

输出格式

Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 00 , if you do not need to perform any operations.

输入输出样例

输入 #1复制

输出 #1复制

输入 #2复制

输出 #2复制

输入 #3复制

输出 #3复制

说明/提示

In the first sample, at first you need to reverse substring [1 \dots 2][1…2] , and then you need to invert substring [2 \dots 5][2…5] .

Then the string was changed as follows:

«01000» \to→ «10000» \to→ «11111».

The total cost of operations is 1 + 10 = 111+10=11 .

In the second sample, at first you need to invert substring [1 \dots 1][1…1] , and then you need to invert substring [3 \dots 5][3…5] .

Then the string was changed as follows:

«01000» \to→ «11000» \to→ «11111».

The overall cost is 1 + 1 = 21+1=2 .

In the third example, string already consists only of ones, so the answer is 00

题解:

我们要把01串变成1串,本质上是做什么?

最朴实的做法,是把所有0全部取反。

但是我们要求最小代价,所以这么裸着做肯定是不行。

所以我们开始认真看一下这个“翻转”操作。

什么时候我们用反转是比较优惠的呢?

就是,因为每次变化是不限量的,所以我们出于贪心考虑,一定是把这个串的0都归拢到一起,这样就可以用一次取反操作直接变成1串。

所以我们取反,其实就是归拢0的一个过程。

但是我们还会考虑这么一个问题:就是当我们的x比y大的话,取反就特别浪费。我们还莫不如直接把他们直接一段段取反,这样还会比反转后取反节约一点。

所以我们统计1之间夹0的段数,用long long 输出即可。

代码:

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cstring>
  4. #define ll long long
  5. #define maxn 300001
  6. using namespace std;
  7. int cnt,n,x,y;
  8. char str[maxn];
  9. int main()
  10. {
  11. scanf("%d%d%d",&n,&x,&y);
  12. scanf("%s",str+1);
  13. str[0]='1';
  14. for(int i=1;i<=n;i++)
  15. if(str[i]=='0' && str[i-1]=='1')
  16. cnt++;
  17. if(cnt==0)
  18. puts("0");
  19. else
  20. printf("%lld\n",(ll)(cnt-1)*min(x,y)+y);
  21. return 0;
  22. }

洛谷 CF997A Convert to Ones的更多相关文章

  1. 洛谷CF997A:Convert to Ones

    温馨提示: 本题适合先思考再看题解,相信各位神犇都能轻轻松松过掉它. 题目链接: https://www.luogu.com.cn/problem/CF997A 分析: 首先要读懂题,to ones, ...

  2. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

  3. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

  4. 洛谷P1108 低价购买[DP | LIS方案数]

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

  5. 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP

    题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...

  6. 洛谷P1710 地铁涨价

    P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交  讨论  题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...

  7. 洛谷P1371 NOI元丹

    P1371 NOI元丹 71通过 394提交 题目提供者洛谷OnlineJudge 标签云端评测 难度普及/提高- 提交  讨论  题解 最新讨论 我觉得不需要讨论O long long 不够 没有取 ...

  8. 洛谷P1538迎春舞会之数字舞蹈

    题目背景 HNSDFZ的同学们为了庆祝春节,准备排练一场舞会. 题目描述 在越来越讲究合作的时代,人们注意的更多的不是个人物的舞姿,而是集体的排列. 为了配合每年的倒计时,同学们决定排出——“数字舞蹈 ...

  9. 洛谷八月月赛Round1凄惨记

    个人背景: 上午9:30放学,然后因为学校举办读书工程跟同学去书城选书,中午回来开始打比赛,下午又回老家,中间抽出一点时间调代码,回家已经8:50了 也许是7月月赛时“连蒙带骗”AK的太幸运然而因同学 ...

随机推荐

  1. nginx小知识

    What Nginx是一款轻量级的Web服务器.反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用. 反向代理 当我们在外网访问百度的时候,其实会进行一个转发,代理到内 ...

  2. Comet OJ - Contest #7 C 临时翻出来的题(容斥+状压)

    题意 https://www.cometoj.com/contest/52/problem/C?problem_id=2416 思路 这里提供一种容斥的写法(?好像网上没看到这种写法) 题目要求编号为 ...

  3. [转载]3.2 UiPath鼠标操作文本的介绍和使用

    一.鼠标(mouse)操作的介绍 模拟用户使用鼠标操作的一种行为,例如单击,双击,悬浮.根据作用对象的不同我们可以分为对元素的操作.对文本的操作和对图像的操作 二.鼠标对文本的操作在UiPath中的使 ...

  4. golang中判断两个slice是否相等

    在golang中我们可以轻松地通过==来判断两个数组(array)是否相等,但遗憾的是slice并没有相关的运算符,当需要判断两个slice是否相等时我们只能另寻捷径了. slice相等的定义 我们选 ...

  5. orale数据库.实例.表空间.用户.表

    近期因为工作原因接触到Oracle数据库.了解到Oracle和mysql的结构上还是有很大的区别的. Oracle数据库---实例---表空间---用户---表 我们将从这5个方面来了解Oracle ...

  6. 如何将vscode的终端放到右侧

    这个问题我之前上百度搜了一下,只有百度经验有, 但是百度经验的图片看不清,而且按照百度经验的回答,我找不到它说的用户设置 于是我问了一下同学,很简单. 右击终端那条框,我们可以得到下面这张截图 点击将 ...

  7. 在linux上安装taiga

    # taiga 安装配置 1.简介 本文档介绍了如何部署完整的Taiga服务(每个模块都是Taiga平台的一部分). Taiga平台由三个主要组件组成,每个组件在编译时和运行时都有自己的依赖关系: t ...

  8. luogu p2705 小球

    题目部分 题目描述 有 R 个红色盒子和 B 个蓝色盒子,还有 R 个红色小球和 B 个蓝色小球.每个盒子只能装一个小球,每个小球都要放在一个盒子里. 如果把一个红色小球放在一个红色盒子里,那么得分是 ...

  9. kubernetes之coredns玩法

    一.概述 新版本的kubernetes默认使用了coredns,这里就不赘述了.直达车:https://coredns.io/.https://kubernetes.io/docs/tasks/adm ...

  10. 图解微信小程序---获取电影信息

    图解微信小程序---获取电影信息 代码笔记 第一步:编写js文件,调用api获取相对应电影详情信息(注意带入的参数是id不在是榜单的type,电影api的movie后面又斜杠,别忘了,对应的绑定数据的 ...