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 ...
随机推荐
- kill 指令的执行原理
kill 指令有两种写法 " kill query + 线程 id "." kill connection(可缺省) + 线程 id ".分别表示关闭指定线程正 ...
- leetcode 1593. 拆分字符串使唯一子字符串的数目最大(DFS,剪枝)
题目链接 leetcode 1593. 拆分字符串使唯一子字符串的数目最大 题意: 给你一个字符串 s ,请你拆分该字符串,并返回拆分后唯一子字符串的最大数目. 字符串 s 拆分后可以得到若干 非空子 ...
- ctfhub技能树—RCE—综合过滤练习
打开靶机 查看页面信息 查看源码可以发现这一次过滤了很多东西,查看当前目录信息 查询到%0a为换行符,可以利用这个url编码进行命令注入,开始尝试 http://challenge-2a4584dab ...
- 对于Update Function Modules的一点说明
To be able to call a function module in an update work process, you must flag it in the Function Bui ...
- [Usaco2008 Feb]Line连线游戏
题目描述 Farmer John最近发明了一个游戏,来考验自命不凡的贝茜.游戏开始的时 候,FJ会给贝茜一块画着N (2 <= N <= 200)个不重合的点的木板,其中第i个点 的横.纵 ...
- HTTP Keep-Alive模式客户端与服务器如何判定传输完成
目录 长连接是什么 服务器如何知道已经完全接受客户端发送的数据 客户端如何知道已经完全接受服务端发送的数据 Transfer-Encoding transfer-coding与Content-Leng ...
- Flask扩展点总结(信号)
信号(源码) 信号,是在flask框架中为我们预留的钩子,让我们可以进行一些自定义操作. pip3 install blinker 根据flask项目的请求流程来进行设置扩展点 1.中间件 from ...
- HTML5与CSS3知识点总结
好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star 原文链接:https://blog.csdn.net/we ...
- win server 2019服务器的iis配置以及网站的简单发布
1.首先远程连接到服务器 2.打开服务器管理器 3添加角色和功能 4.安装类型:选择基于角色或基于功能的安装 →服务器角色:从服务器池中选择服务器 5.服务器角色选择Web服务器(iis) 6.功能 ...
- (17)-Python3之--文件操作
1.文件的操作流程 第一,建立文件对象. 第二,调用文件方法进行操作. 第三,不要忘了关闭文件.(文件不关闭的情况下,内容会放在缓存,虽然Python会在最后自动把内容读到磁盘,但为了以防万一,要养成 ...