Codeforces Round #295 (Div. 2) B. Two Buttons (DP)
题意:有两个正整数\(n\)和\(m\),每次操作可以使\(n*=2\)或者\(n-=1\),问最少操作多少次使得\(n=m\).
题解:首先,若\(n\ge m\),直接输出\(n-m\),若\(2*n>=m\),分\(m\)的奇偶判断一下,如果是奇数就输出\(n-(m+1)/2+2\),是偶数就输出\(n-m/2+1\).否则我们就需要用dp来求解,因为是求最小值,所以先初始化将所有值设为\(INF\),\(dp[i]\)表示从\(n\)到\(m\)的操作次数最少的最优解,首先需要更新\([1,n]\)的状态,这个不难写,\(dp[i]=dp[i+1]+1\),然后我们就可以从\(1\)开始枚举到\(m\),而我们当前的状态\(dp[i]\)可以更新后面的状态\(dp[i*2]\),这步应该不难想,这儿的难点是我们需要更新一些奇数的状态,比如\(n=2\),我们刚开始可以更新\(dp[4]\),然后到\(n=3\)的时候发现\(3\)只能通过\(4\)更新得到,而\(dp[4]\)由\(dp[2]\)更新过了,所以我们可以通过\(dp[4]\)来更新\(dp[3]\),于是每次遍历我们更新两个状态,一个是自己的状态\(dp[i]=min(dp[i],dp[i+1]+1)\),一个是后面的数的状态\(dp[i*2]=min(dp[i*2],dp[i]+1)\).
代码:
int n,m;
int dp[N]; int main() {
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
n=read(),m=read(); if(n>=m){
printf("%d\n",n-m);
return 0;
} if(m<=2*n){
if(m&1){
int cnt=(m+1)/2;
printf("%d\n",n-cnt+2);
}
else printf("%d\n",n-m/2+1);
return 0;
}
me(dp,INF,sizeof(dp));
dp[n]=0;
for(int i=n-1;i>=1;--i) dp[i]=dp[i+1]+1; for(int i=1;i<=m;++i){
dp[i]=min(dp[i],dp[i+1]+1);
dp[i*2]=min(dp[i*2],dp[i]+1);
} printf("%d\n",dp[m]); return 0;
}
Codeforces Round #295 (Div. 2) B. Two Buttons (DP)的更多相关文章
- Codeforces Round #295 (Div. 2)B - Two Buttons BFS
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #295 (Div. 2)---B. Two Buttons( bfs步数搜索记忆 )
B. Two Buttons time limit per test : 2 seconds memory limit per test :256 megabytes input :standard ...
- Codeforces Round #295 (Div. 2) B. Two Buttons
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #295 (Div. 2) B. Two Buttons 520B
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 【记忆化搜索】Codeforces Round #295 (Div. 2) B - Two Buttons
题意:给你一个数字n,有两种操作:减1或乘2,问最多经过几次操作能变成m: 随后发篇随笔普及下memset函数的初始化问题.自己也是涨了好多姿势. 代码 #include<iostream> ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- Codeforces Round #295 (Div. 2)
水 A. Pangram /* 水题 */ #include <cstdio> #include <iostream> #include <algorithm> # ...
- codeforces 521a//DNA Alignment// Codeforces Round #295(Div. 1)
题意:如题定义的函数,取最大值的数量有多少? 结论只猜对了一半. 首先,如果只有一个元素结果肯定是1.否则.s串中元素数量分别记为a,t,c,g.设另一个串t中数量为a',t',c',g'.那么,固定 ...
- Codeforces Round #295 (Div. 2)C - DNA Alignment 数学题
C. DNA Alignment time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
随机推荐
- 【译】Async/Await(三)——Aysnc/Await模式
原文标题:Async/Await 原文链接:https://os.phil-opp.com/async-await/#multitasking 公众号: Rust 碎碎念 翻译 by: Praying ...
- uni-app请求uni.request封装使用
对uni.request的一些共同参数进行简单的封装,减少重复性数据请求代码.方便全局调用. 先在目录下创建 utils 和 common 这2个文件夹 utils 是存放工具类的,common 用来 ...
- 1.kafka基础架构
kafka基础架构 ## 什么是kafka? Kafka是一个分布式的基于发布/订阅模式的消息队列,主要应用于大数据实时处理领域. 1.什么是消息队列? 2.使用消息队列的好处 1)解耦 允许你独立的 ...
- C # 9.0的record
官方消息: c # 9.0已经过时了!早在五月份,我就在博客中介绍了 c # 9.0计划,下面是该文章的更新版本,以便与我们最终发布的计划相匹配. 对于每一个新的 c # 版本,我们都在努力提高常见编 ...
- 这几个小技巧,让你书写不一样的Vue!
前言 最近一直在阅读Vue的源码,发现了几个实战中用得上的小技巧,下面跟大家分享一下. 同时也可以阅读我之前写的Vue文章 vue开发中的"骚操作" 挖掘隐藏在源码中的Vue技巧! ...
- FlightGear 从输出所省略的额外重寻址溢出
2020-12-27 在龙芯Fedora28上编译 FlightGear 2019.1.1 时遇到 从输出所省略的额外重寻址溢出 错误,错误信息如下: [ 98%] Linking CXX execu ...
- POJ 3461__KMP算法
[题目描述] 法国作家乔治·佩雷克(Georges Perec,1936-1982)曾经写过一本书,<敏感字母>(La disparition),全篇没有一个字母'e'.他是乌力波小组(O ...
- CODEVS 2542单词__fail树
2542 单词 2013年省队选拔赛天津市队选拔赛 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 小张最近在忙毕 ...
- 十三:SpringBoot-基于Yml配置方式,实现文件上传逻辑
SpringBoot-基于Yml配置方式,实现文件上传逻辑 1.文件上传 2.搭建文件上传界面 2.1 引入页面模板Jar包 2.2 编写简单的上传页面 2.3 配置页面入口 3.SpringBoot ...
- (十四)整合 ClickHouse数据库,实现数据高性能查询分析
整合 ClickHouse数据库,实现数据高性能查询分析 1.ClickHouse简介 1.1 数据分析能力 2.SpringBoot整个ClickHouse 2.1 核心依赖 2.2 配属数据源 2 ...