【打CF,学算法——二星级】CF 520B Two Buttons
提交链接:Two Buttons
题面:
2 seconds
256 megabytes
standard input
standard output
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button,
 device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n.
Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result?
The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104),
 separated by a space .
Print a single number — the minimum number of times one needs to push the button required to get the number m out of number n.
4 6
2
10 1
9
In the first example you need to push the blue button once, and then push the red button once.
In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
解题:
法一:
一開始看到这题,感觉似曾相识。感觉可能能够写,最初的想法是这种。n>=m。直接输出n-m。当n<m时。m若为偶数除2,m为奇数+1除以2。一直分解m到1。并记录这个过程中m分解的过程。然后将n减到近期的m分解过程中的m上。感觉非常不靠谱。纯瞎想。结果别人敲了一下说是对的。只是不是必需这么复杂,循环 m是偶数 除以2 计数值+1 。m是奇数 加一除以2 计数值+2 直到m<n 。 最后计数值加上n-m就好了 尽管不知道为什么这样就对。可是感觉是让减法尽量早减,这样就能够降低减法的次数。
代码:
#include <iostream>
using namespace std;
int main()
{
int n,m,cnt=0,t,ans=0,last;
cin>>n>>m;
if(n>=m)cout<<n-m<<endl;
else
{
while(m>n)
{
if(m%2==0)
{
ans++;
m/=2;
}
else
{
m=(m+1)/2;
ans+=2;
}
}
ans+=(n-m);
cout<<ans<<endl;
}
return 0;
}
法二:
看了一篇题解。说是spfa爆搜,顿时给跪了。感觉好奇怪。我也没看详细题解,想了一下。是不是建一张图,10000个数,减法由x到x-1建立一条权值为1的边。乘法由n到2*n建立一条权值为1的边,最后仅仅要搜索n到m的最短路就好了。
这是我自己的想法。感觉非常巧妙,可是不会超复杂度吗?然而边不是矩阵存储的,由于10000个数。最多不会超过20000条边。
【打CF,学算法——二星级】CF 520B Two Buttons的更多相关文章
- 【打CF,学算法——二星级】Codeforces Round #313 (Div. 2) B. Gerald is into Art(水题)
		[CF简单介绍] 提交链接:http://codeforces.com/contest/560/problem/B 题面: B. Gerald is into Art time limit per t ... 
- 【打CF,学算法——二星级】Codeforces Round #312 (Div. 2)  A  Lala Land and Apple Trees
		[CF简单介绍] 提交链接:A. Lala Land and Apple Trees 题面: A. Lala Land and Apple Trees time limit per test 1 se ... 
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
		1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ... 
- 协同过滤(CF)算法
		1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ... 
- 1164: 零起点学算法71——C语言合法标识符(存在问题)
		1164: 零起点学算法71——C语言合法标识符 Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: 10 ... 
- 1163: 零起点学算法70——Yes,I can!
		1163: 零起点学算法70--Yes,I can! Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: ... 
- 1147: 零起点学算法54——Fibonacc
		1147: 零起点学算法54--Fibonacc Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: 20 ... 
- 1145: 零起点学算法52——数组中删数II
		1145: 零起点学算法52--数组中删数II Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: 293 ... 
- 1137: 零起点学算法44——多组测试数据输出II
		1137: 零起点学算法44--多组测试数据输出II Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: ... 
随机推荐
- jvm内存分区
			java内存是由jvm进行管理的,其内存简易模型如下图: jvm管理的内存大体上可分为方法区.堆.程序计数器.线程栈.本地方法区这几部分.方法区:主要存放类的元信息(包括类的名称.修饰符.静态变量.f ... 
- can't set blob value on that column
			MySQL_Prepared_Statement::setBlob: can't set blob value on that column, MySQL error code:0, SQLState ... 
- Tomcat无法clean,无法remove部署的项目
			错误: 对部署在Tomcat下的项目进行clean操作,总是提示Could not load the Tomcat server configuration,错误信息如图: 解决: 原来是将Serve ... 
- DIV水平 垂直居中CSS
			/*实现一.原理:要让div等块级元素水平和垂直居中,必需知道该div等块级元素的宽度和高度,然后设置位置为绝对位置,距离页面窗口左边框和上边框的距离设置为50%,这个50%就是指页面窗口的宽度和高度 ... 
- 梦想CAD控件COM接口标注样式
			增加标注样式 用户可以增加标注样式到数据库,具体实现c#代码如下: private void CreateDim() { //返回控件的数据库对象 MxDrawDatabase database = ... 
- ThinkPHP---TP功能类之上传
			[一]概论 (1)上传操作的核心操作:移动临时文件(move_upload_file),在ThinkPHP里封装了上传类Upload.class.php (2)上传类Upload.class.php代 ... 
- 17Oracle Database 维护
			Oracle Database 维护 备份 还原 
- Deepin系统关于每次启动终端都要输入source /etc/profile的问题
			关于每次启动终端都要输入source /etc/profile的问题 当我在Deepin系统中下载了node以及npm之后,我为了将node导入到系统文件,使用了以下命令sudo gedit ``/e ... 
- 后台中的sql注入
			aa.getSqlMap().put("order"," and a.id not in(\'"+po.getId()+"\')"); \' ... 
- 剑指offer---正则表达式匹配
			题目:正则表达式匹配 要求:请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所 ... 
