【盗版动归】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 ...
随机推荐
- mysql基础-新版5.7.10源码安装-记录(一)
0x01 MySQL 从 5.5 版本开始,通过 ./configure 进行编译配置方式已经被取消,取而代之的是 cmake 工具 引用一句话 cmake的重要特性之一是其独立于源码(out-of- ...
- 第一次使用Genymotion遇到的问题:for an unknown reson,VirtualBox DHCP has not assigned an IP address to virtual
解决方案:http://www.aiuxian.com/article/p-554135.html
- k8s学习-存储
4.6.存储 4.6.1.ConfigMap 创建方式 文件夹/文件创建 mkdir dir cd dir cat > c1.properties <<EOF c1.name=c1 ...
- Python教程:matplotlib 绘制双Y轴曲线图
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:数据皮皮侠 双X轴的可以理解为共享y轴 ax1=ax.twiny() ...
- pip install 执行过程中遇到的各种问题
一.pip install 安装指定版本的包 要用 pip 安装指定版本的 Python 包,只需通过 == 操作符 指定. pip install robotframework == 2.8.7 将 ...
- 记录一次更改服务器名称导致mysql 不能正常登录、启动
由于客户要求更改服务器的名称,以便区分多台服务器:修改前mysql 能正常登录,但是修改后,登录时报错: Enter password: ERROR 1524 (HY000): Plugin '*C6 ...
- [CF696D]Legen...
题目 点这里看题目. 分析 首先对于模式串建立 AC 自动机,并且计算出每个状态\(p\)的贡献总和\(con(p)\). 考虑一个朴素的 DP : \(f(i,p)\):当前串长度为 ...
- spark源码解析总结
========== Spark 通信架构 ========== 1.spark 一开始使用 akka 作为网络通信框架,spark 2.X 版本以后完全抛弃 akka,而使用 netty 作为新的网 ...
- 使用python求解向量值函数的雅各比(Jacobian)矩阵
考虑一个向量值函数$R^m \rightarrow R^n$,即$\textbf{y} = f(\textbf{x})$,它的雅各比(Jacobian)矩阵定义如下. 下面记录下一段使用python求 ...
- 在android项目中使用FontAwesome字体
在android项目中使用FontAweSome图标集,可以方便的适配各种屏幕分辨率,不必在各种不同分辨率文件夹中新建资源文件.使用字体是一种很巧妙的方法,把图像以字体的方式呈现,这样以前设置为and ...