【打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: ...
随机推荐
- LN : leetcode 238 Product of Array Except Self
lc 238 Product of Array Except Self 238 Product of Array Except Self Given an array of n integers wh ...
- Font Awesome 图标使用总结
参考 http://fontawesome.dashgame.com/ 1 大图标递进 fa-lg (33%递增).fa-2x. fa-3x.fa-4x,或者 fa-5x 2 固定宽度 fa-f ...
- python--12、索引知识
MySQL索引及优化 影响性能的因素 需求:一个论坛帖子总量的统计,附加要求:实时更新.从功能上来看非常容易实现,执行一条 SELECT COUNT(*) from 表名 的 Query 就可以得到结 ...
- 关于加减和es6
console.log(1+ "2"+"2"); //对于加法来说,如果只有一个操作数是字符串,则将另一个操作数也转换为字符串,然后将两者拼接,为122 c ...
- 利用eclipse调试JDK源码
先看效果图 综合网上各种教程,总结如下 新建 D:/jdk/src .D:/jdk/debug 目录 src存放源码 debug存放编译结果 将 %JAVA_HOME%/src.zip 解压到 D:/ ...
- Python批量下载电视剧电影--自己动手丰衣足食
前言 为了看美剧<天蝎>,在电影天堂找到了,于是就想下载下来好好欣赏. 废话不说了,直接上代码. 代码 import requests,re,os,time url = "htt ...
- dubbo之服务容器
服务容器是一个standalone的启动程序,因为后台服务不需要Tomcat或JBoss等Web容器的功能,如果硬要用Web容器去加载服务提供方,增加复杂性,也浪费资源. 服务容器只是一个简单的Mai ...
- RabbitMQ系列(八)--顺序消费模式和迅速消息发送模式
MQ使用过程中,有些业务场景需要我们保证顺序消费,而如果一个Producer,一个Queue,多个Consumer的情况下是无法保证顺序的 举例: 1.业务上产生三条消息,分别是对数据的增加.修改.删 ...
- c# 常用 Common
/// <summary> /// md5加密字符串 /// </summary> /// <param name="message">< ...
- java解析从接口获取的json内容并写到excle(只写与标题匹配的值,并非把所有的接口返回值都写进去)
需求:从接口中获取的一个json数组中有多个对象,每个对象中的值并非都需要,只需查出标题中的几项对应的值即可.且还需要按某个字段排序后依次写到excel 实现方法如下: package jansonD ...