• 题意:有两个正整数\(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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 【记忆化搜索】Codeforces Round #295 (Div. 2) B - Two Buttons

    题意:给你一个数字n,有两种操作:减1或乘2,问最多经过几次操作能变成m: 随后发篇随笔普及下memset函数的初始化问题.自己也是涨了好多姿势. 代码 #include<iostream> ...

  6. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

  7. Codeforces Round #295 (Div. 2)

    水 A. Pangram /* 水题 */ #include <cstdio> #include <iostream> #include <algorithm> # ...

  8. codeforces 521a//DNA Alignment// Codeforces Round #295(Div. 1)

    题意:如题定义的函数,取最大值的数量有多少? 结论只猜对了一半. 首先,如果只有一个元素结果肯定是1.否则.s串中元素数量分别记为a,t,c,g.设另一个串t中数量为a',t',c',g'.那么,固定 ...

  9. 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 ...

随机推荐

  1. 一道有趣的golang排错题

    很久没写博客了,不得不说go语言爱好者周刊是个宝贝,本来想随便看看打发时间的,没想到一下子给了我久违的灵感. go语言爱好者周刊78期出了一道非常有意思的题目. 我们来看看题目.先给出如下的代码: p ...

  2. Linux 防火墙基于 CentOS7 的防火墙操作命令

    防火墙服务操作命令 重启防火墙 systemctl restart firewalld 查看防火墙状态 systemctl status firewalld 开启.关闭.重启防火墙 # 开启 serv ...

  3. 1V转3.3V稳压供电的芯片电路图

    1V转3.3V供电是简单的,仅需要一个芯片和三个外围元件即可组成这样的一个1V转3.3V的电路图和升压电路了.可以持续稳定地供电3.3V给模块或者MCU灯电路.让后端工作稳定,同时也能控制电路的功耗. ...

  4. Lakehouse: 统一数据仓库和高级分析的新一代开放平台

    1. 摘要 数仓架构在未来一段时间内会逐渐消亡,会被一种新的Lakehouse架构取代,该架构主要有如下特性 基于开放的数据格式,如Parquet: 机器学习和数据科学将被作为头等公民支持: 提供卓越 ...

  5. UI测试框架

    1. 从上到下共分成4层: 用例层  组件管理层  元素管理层  公共数据层 2. 用例层: 将每条用例使用参数化, 公共参数存储到"公共数据层", 中间参数通过组件层传递 3. ...

  6. https://nginx.org/en/docs/http/request_processing.html

    https://nginx.org/en/docs/http/request_processing.html

  7. (Oracle)误删oracle表结构恢复

    在操作数据库时,我们常常会不小心把表结构删除了.有时候建表很麻烦大到100多个字段,而又找不到当初的建表语句.其实这时候不用担心,oracle和咱们widows一样,他也有个回收站,只要你没有清除回收 ...

  8. 加密填补 填充 pad padding

    RFC 1423 - Privacy Enhancement for Internet Electronic Mail: Part III: Algorithms, Modes, and Identi ...

  9. 丢包 ICMP

    小结: 1.ICMP 常见网络丢包故障分析及处理 云极安 云极安 2019-12-25 我们在管理维护网络的过程中经常会遇到数据包丢失的现象.使用Ping命令进行连通性测试,则会发现Ping包延时远远 ...

  10. Python基础(列表中变量与内存关系)

    在Python中,copy的是内存地址,引用的是列表的引用地址,列表里存的是各个元素的地址 例如: name = [1,2,3,4,['xfxing','summer',6]] n2 = name.c ...