【盗版动归】Codeforces998C——Convert to Ones 归一操作
嘤嘤嘤,因为最近文化课老师追的紧了+班主任开班会,所以这博客是赶制的赝品
题目:
You've got a string a1,a2,…,ana1,a2,…,an, consisting of zeros and ones.
Let's call a sequence of consecutive elements ai,ai + 1,…, ajai,ai + 1,…, aj (1≤ i≤ j≤ n1≤ i≤ j≤ 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» →→ «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» →→ «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?
Input
The first line of input contains integers nn, xx and yy (1 ≤ n ≤ 300000,0≤x,y≤1091 ≤ n ≤ 300000,0≤x,y≤109) — 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.
Output
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.
Examples
5 1 10
01000
11
给定一串0-1序列,定义两种操作:
- 操作一:选取一连续子串倒置。
- 操作二:选取一连续子串把进行01互换(取反)。
- 并给出操作一和操作二的代价,分别为x和y。
操作到最后要把串变成只含1的串,问最小的操作代价。
思路:
一看到这道题,脑子里想的是区间DP建立二维数组f,f[i][j]表示把i~j都转化为1的最小代价,后来因为转移方程过于复杂,且时间开销大了,所以直接放弃,然后搜了一下题解,一看标题说是假动归,我立即转向普通暴力。
读题可以了解到,1100和10是在翻转和反转下是等价的,连续的0或1可以直接转化成一个0或一个1,如11100110可以直接变为1010,这样他们的代价是一样的。然后蒟蒻提出了第一个思路:统计压缩后的0和1的数目,然后根据0和1的数目进行选择,代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn=+,INF=0x3f3f3f3f;
int n,m,c1,a[maxn],c2,low[maxn],f[maxn],len1,len0,len;
inline int read(){
int s=,w=;
char ch=getchar();
while(ch<''||ch>''){if(ch=='-')w=-;ch=getchar();}
while(ch>=''&&ch<='') s=s*+ch-'',ch=getchar();
return s*w;
}
int main(){
//freopen("a.in","r",stdin);
n=read(),c1=read(),c2=read();
for(int i=;i<=n;i++){
char x;
cin>>x;
x-='';
if(i==||x!=a[len]){
a[++len]=x;
if(a[len]==)len1++;
else if(a[len]==)len0++;
}
}
if(len0==){
cout<<"";
return ;
}
if(len1==){
cout<<c2;return ;
}
int ans=0x3f;
if(len1<len0)cout<<min((len0-1)*c1+c2,c2*len0);
else if(len1>len0)cout<<min(len0*c1+c2,c2*len0);
else cout<<min(len0*c1+c2,c2*len0);
}
提交了发现有丶问题,这三个判断十分相似,貌似是写错了,其实可以合并,然后就改成了下面的极简代码:
int main(){
freopen("a.in","r",stdin);
scanf("%d%d%d %s",&n,&c1,&c2,a+);
a[]='';//你问我为什么这里变成了scanf?因为提交多次发现卡死在了test4,我以为是快读beng了(遇到过卡快读的题)
for(int i=;i<=n;i++){
if(a[i-]==''&&a[i]=='')len0++;
}
if(len0==){
printf("");
}else printf("%lld",(long long)min(c1*(len0-1)+c2,c2*len0);//卡在test4的原因:不开long long
return ;
}
解释:
可以通过模拟找规律得到最后的式子
对于这个0和1分隔开的串,就结果来看,我们可以把所有的0放到一边,最后把连续的0变成1,代价为c1*(len0-1)+c2,也可以把分隔的0全部转化为1,代价为c2*len0。
然后我看了眼其他人的想法,比我的要详细一些:第一种方法,我们可以选择第二段 10 ,对其进行倒置操作,所以整个串就变成了 001101010。然后再次合并相邻的1 和相邻的0,原串变成了0101010。然后在进行一次相同的操作,就变成了 01010.以此类推,最后将变成 0 10,再倒置一次,变成 00 1,然后再对00 进行一次操作二即可。这种方法的花费为 (len0-1)*c1+c2;第二种方法,我们直接对每一段0实施操作二,使得其变为全1串,这样的花费是len0*c2(原地址https://blog.csdn.net/qq_44549690/article/details/102498866?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3(怎么这么长))
嘤嘤嘤,没了。晚上完善一下线型DP模板的题解和打地鼠的题解
【盗版动归】Codeforces998C——Convert to Ones 归一操作的更多相关文章
- C# 动软生成器对应的Access数据库操作类DbHelperOleDb
using System;using System.Collections;using System.Collections.Specialized;using System.Data;using S ...
- 转发自:一像素 十大经典排序算法(动图演示)原链接:https://www.cnblogs.com/onepixel/articles/7674659.html 个人收藏所用 侵删
原链接:https://www.cnblogs.com/onepixel/articles/7674659.html 个人收藏所用 侵删 0.算法概述 0.1 算法分类 十种常见排序算法可 ...
- 【手势交互】6. 微动VID
中国 天津 http://www.sharpnow.com/ 微动VID是天津锋时互动科技有限公司开发的中国Leap Motion. 它能够识别并跟踪用户手部的姿态.包含:指尖和掌心的三维空间位置:手 ...
- apple iMac一体机 装双系统 实战! (Apple +Win 7 64bit)
http://group.zol.com.cn/1/641_485.html http://tieba.baidu.com/p/2532811864 http://www.jb51.net/os/82 ...
- DDD 领域驱动设计-看我如何应对业务需求变化,愚蠢的应对?
写在前面 阅读目录: 具体业务场景 业务需求变化 "愚蠢"的应对 消息列表实现 消息详情页实现 消息发送.回复.销毁等实现 回到原点的一些思考 业务需求变化,领域模型变化了吗? 对 ...
- 超越 JSON: Spearal 序列化协议简介
Spearal 是一个新的开源的序列化协议,这个协议旨在初步替换JSON 将HTML和移动应用连接到Java的后端. Spearal的主要目的是提供一个序列协议,这个协议即使是在端点间传输的复杂的 ...
- Codeforces 710F String Set Quries
题意 维护一个字符串的集合\(D\), 支持3种操作: 插入一个字符串\(s\) 删除一个字符串\(s\) 查询一个字符串\(s\)在\(D\)中作为子串出现的次数 强制在线 解法 AC自动机+二进制 ...
- 基于fab自动化部署
fab是一个python库,强大好使,可以做很多帮助你减轻工作量的事情,比如在多台服务器上部署web项目,这里就讲讲使用它简单的方法来执行部署的过程. 关于fab的安装的基本使用,网上一搜一大把,内容 ...
- canvas动画
1.动画主要是requestAnimationFrame方法,现在我们来一步步实现一个在画布内滚动的实例. html代码: <canvas id="canvas" width ...
随机推荐
- 搞清楚C语言指针
Part 0:为什么要写这篇文章 C语言中的指针是C语言的精髓,也是C语言的重难点之一. 然而,很少有教程能把指针讲的初学者能听懂,还不会引起歧义. 本文章会尝试做到这一点,如有错误,请指出. Par ...
- OV2640读ID全是FF问题
最近刚好在Cyclone IV上通过LVDS把一个7寸的屏点亮,赶着热度,淘宝买了OV2640这个摄像头模块,初始化因为用Vrilog比C复杂得多,易调试性不如C,所以使用STM32初始化,模块有F3 ...
- springboot使用自定义异常
sprinboot使用自定义注解 创建自定义异常类,继承RuntimeException public class MyException extends RuntimeException { p ...
- 学习第一个python程序
打印9*9惩罚表 for i in range(1,10): for j in range(1,i+1): print(str(j)+"*"+str(i)+"=" ...
- easypoi 读取 Excel 简单应用
背景 在做接口测试的时候,经常会使用 Excel 来存储对应的接口信息和用例信息,为了方便程序的读取,引入easypoi 工具来读取 Excel 内容.easypoi 比起 poi 使用更加的方便,代 ...
- 关于MYSQL的查询时间段、删除数据、查询目标行的命令用法。
Q1.第一次工作写命令时,写了一个把一个表的数据插入到另一个表中,运行时命令长时间处于执行状态. A.第一次处理数据库大量数据,长时间按运行属于正常现象,这与学校中的小数据不同. Q2.如何查询数据库 ...
- WebDriverWait与expected_conditions结合使用
expected_conditions判断页面元素 demo2 from selenium import webdriver from selenium.webdriver.support.ui im ...
- 说说硬件中核心板的作用和优缺点,基于i.MX8M Mini核心处理器平台
核心板,顾名思义,即硬件构成中关键的器件和电路打包封装的一块电子主板,具有布线复杂.多层.高频信号干扰.器件密度高等特性,大多数核心板集成了处理器.内存.存储器.电源管理和引脚,通过引脚与配套基板连接 ...
- position两种绝对定位的区别
position绝对定有两种,分别为absolute和fixed 一.共同点: 1.改变行内元素的呈现方式,display被置为inline:block 2.让元素脱离普通流,不占据空间 3.默认会覆 ...
- cb24a_c++_STL算法简介
算法概述 算法部分主要由头文件<algorithm>,<numeric>和<functional>组成. <algorithm>是所有STL ...