Codeforces 997A Convert to Ones(思维)
https://codeforces.com/problemset/problem/997/A
题目大意:
给定一串0-1序列,定义两种操作:
操作一:选取一连续串倒置。
操作二:选取一连续串把进行01互换(取反)。
并给出操作一和操作二的代价,分别为x和y。
操作到最后要把串变成只含1的串,问最小的操作代价。
假定连续0的段数是num,那么可以知道,每进行一次操作一,就可以减少一次操作二的次数。
因此就要考虑操作一和二的优先使用问题:
如果x<y 就优先倒置,把所有0块区间合成一个0块区间,然后取反,代价(num-1)*x+y
如果x>y 直接把每个0块区间取反好了,代价 num*y
- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- #include <string>
- #include <math.h>
- #include <algorithm>
- #include <vector>
- #include <stack>
- #include <queue>
- #include <set>
- #include <map>
- #include <math.h>
- const int INF=0x3f3f3f3f;
- typedef long long LL;
- const int mod=1e9+;
- #define Bug cout<<"---------------------"<<endl
- const int maxn=3e5+;
- using namespace std;
- char str[maxn];
- int main()
- {
- int n,x,y;
- scanf("%d %d %d",&n,&x,&y);
- int op = x<=y? :;
- scanf("%s",str);
- int pre = ;//上一个字符
- int num = ;//连续0段数
- for(int i = ;i < n;i++)
- {
- if(str[i] == '' && pre == )
- num++;
- pre = str[i]-'';
- }
- LL ans = ;
- if(op==&&num!=)//优先操作一
- ans=(LL)(num-)*x+y;//注意结果要类型转换为long long
- else
- ans=(LL)num*y;//注意结果要类型转换为long long
- printf("%lld\n",ans);
- return ;
- }
Codeforces 997A Convert to Ones(思维)的更多相关文章
- Codeforces 515C 题解(贪心+数论)(思维题)
题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’ ...
- CodeForces - 427A (警察和罪犯 思维题)
Police Recruits Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Sub ...
- codeforces 895B XK Segments 二分 思维
codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...
- codeforces 893D Credit Card 贪心 思维
codeforces 893D Credit Card 题目大意: 有一张信用卡可以使用,每天白天都可以去给卡充钱.到了晚上,进入银行对卡的操作时间,操作有三种: 1.\(a_i>0\) 银行会 ...
- C. Nice Garland Codeforces Round #535 (Div. 3) 思维题
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- C Alyona and Spreadsheet Codeforces Round #401(Div. 2)(思维)
Alyona and Spreadsheet 这就是一道思维的题,谈不上算法什么的,但我当时就是不会,直到别人告诉了我,我才懂了的.唉 为什么总是这么弱呢? [题目链接]Alyona and Spre ...
- codeforces 848B Rooter's Song 思维题
http://codeforces.com/problemset/problem/848/B 给定一个二维坐标系,点从横轴或纵轴垂直于发射的坐标轴射入(0,0)-(w,h)的矩形空间.给出点发射的坐标 ...
- Codeforces 671A Recycling Bottles(贪心+思维)
题目链接:http://codeforces.com/problemset/problem/671/A 题目大意:给你两个人的位置和一个箱子的位置,然后给出n个瓶子的位置,要求让至少一个人去捡瓶子放到 ...
- Codeforces 617B:Chocolate(思维)
题目链接http://codeforces.com/problemset/problem/617/B 题意 有一个数组,数组中的元素均为0或1 .要求将这个数组分成一些区间,每个区间中的1的个数均为1 ...
随机推荐
- pyhton输出表格数据出现省略号?(教你很快解决)
//2019.07.18 pandas是python提供的非常好用的数据分析模块,但是在使用pandas进行数据分析时,有时候需要查看打印的结果,当dataframe行数或者列数比较多的时候,打印结果 ...
- Centos7安装mysql5.7.21
1.卸载系统自带的 MariaDB [root@CentOS ~]# rpm -qa | grep mariadb mariadb-libs--.el7.x86_64 [root@CentOS ~]# ...
- 【剑指Offer】面试题27. 二叉树的镜像
题目 请完成一个函数,输入一个二叉树,该函数输出它的镜像. 例如输入: 4 / \ 2 7 / \ / \ 1 3 6 9 镜像输出: 4 ...
- 【php】PHP现代框架代表-Laravel框架核心技术特性
一.php语言特点及发展现状 1.服务端脚本语言,自由度太大 ,一个业务逻辑可言写在模型里,控制器里,也可以单独封装成一个类,甚至可以嵌入到html里,这样势必会造成混乱,业务逻辑散落在各处,尤其对于 ...
- OSDA - 一个以MIT协议开源的串口调试助手
市场其实有很多开源的串行端口调试助手(Open Serial Port debug assistant),但其中很大一部分没有明确的开源协议,还有一部分只限个人使用,所以编写了一个并以MIT协议授权开 ...
- unicode字符等价探究
Demobaidu.com(\uff41)能跳转到baidu.combаidu.com(\u0430)不能跳转到baidu.com,被认为成一个其他域名 等价原因两个不同编码的Unicode字符之间可 ...
- Essay写作常见错误精选
Essay写作常见错误精选.Essay写作有许多不为人注意的小细节,如果申请人在这些细节上不注意,往往会犯一些很典型的错误.和小编一起来看看留学Essay写作常见错误解析. 1)直接把申请学校A的Es ...
- SpringMVC:提交日期类型报400错误解决方法
方法1:可以使用@ControllerAdvice增强Controller @ControllerAdvice public class BaseControllerAdvice { // 初始化绑定 ...
- M: Mysterious Conch 字符串哈希
Problem Description小明有一个神奇的海螺,你对海螺说一段字符串,海螺就会返回一个单词,有字符串里面的所有字符组成如告诉海螺“lloeh”海螺则会告诉你“hello”如果有多个单词对应 ...
- dp学习笔记(各种dp,比较杂)
HDU1176 中文题意不多解释了. 建一个二维dp数组,dp[ i ][ j ]表示第 i 秒落在 j 处一个馅饼.我们需要倒着DP,为什么呢,从 0秒,x=5处出发,假如沿数组正着往下走,终点到哪 ...