【盗版动归】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 ...
随机推荐
- java代码(11) ---java代码的优化
java代码的优化 参考了一些Java开发手册有关代码的规范,觉得一段好的代码可以从三个维度去分析.1)性能,2)可扩展性,3)可读性 让我们看看别人是怎么去分析,还有值得我们去学习的地方,也是我正在 ...
- [bx] and loop
1.[bx] 表示一个内存单元,它的偏移地址在bx中 mov al,[bx] 2.描述符号() 来表示一个寄存器或一个内存单元中的内容. 约定符号idata表示常量. 3.loop 标号 CPU在执行 ...
- tensorflow2.0学习笔记第一章第三节
1.3鸢尾花数据读入 # 从sklearn包datasets读入数据 from sklearn import datasets from pandas import DataFrame import ...
- hql 转 sql
import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.hql.ast.QueryTranslatorI ...
- 【转载】图解NumPy
目录 1. 读写文件 2. 内建向量/矩阵 3. 切片操作 4. 聚合函数 4.1. 向量 4.2. 矩阵 5. 矩阵的转置和重构 6. 常用操作API 7. 应用实例 7.1. 生成向量.矩阵 7. ...
- [ARC060D] 最良表現
题目 点这里看题目. 分析 由于 KMP 的失配数组有着天然的找循环节的功能,因此我们不难想到对原串进行两次 KMP ,一正一反. 可以发现如下的规律: 1. 原串无循环节,这个时候 ...
- python+selenium识别图片验证码
import timeimport pytesseractfrom PIL import Image, ImageEnhancefrom selenium import webdriver url = ...
- Java中的map集合顺序如何与添加顺序一样
一般使用map用的最多的就是hashmap,但是hashmap里面的元素是不按添加顺序的,那么除了使用hashmap外,还有什么map接口的实现类可以用呢? 这里有2个,treeMap和linkedH ...
- 附017.Kubernetes_v1.17.4 Dashboard部署
一 Kubernetes dashboard简介 1.1 Web UI简介 dashboard是基于Web的Kubernetes用户界面.可以使用dashboard将容器化应用程序部署到Kuberne ...
- mysql常用基础指令大全
mysql指令 启动 net start mysql 退出mysql quit 登录 mysql -uroot -p 逻辑非 not ! 逻辑与 and && 或者 or || 逻辑异 ...