B. Two Buttons

这是Codeforces Round #295 (Div. 2) 的B 题,题意为:

给出n, m, 有两种操作,n 减一 和 n 乘以 2,问最少要多少次操作才能把n 变成 m。

Sample test(s)
input
4 6
output
2
input
10 1
output
9
input 是 n 和 m ,output 是次数。

可以用BFS来做,在遍历每一层的时候,要记录一下当前的值,并且要防止同一个值被遍历两次。要是遇到当前值是目标值,那么变成当前值的次数必然是最少的。

但是,也可以反过来做,求m变成n,有两种操作m除以2 和 m加1,求最少操作次数。要是遇到m小于n的话,m就加一,因为加一是最好的操作,要是遇到m大于n的话,这里有2种情况,如果m是偶数的话,m就除以2,否则m加1。

这里讨论一下,为什么 m > n && m%2 == 0 时,m 要除以2(m > n 且 m 是奇数时,m不能被2整除,只能加1)

假设:  m > n 且 m 是偶数。

那么m 有两种情况 : ① m >= 2n ; ② 2n > m > n 。

第①种就直接 m 除以2 了,如果m越加 1,到最后还是要除以2,这样次数会越来越多,明显没必要,直接m除以2。

第②种的话,有两种选择: 

A. m加1直到 m == n,操作次数是 2n-m+1(当然m加2后,还可以选择是否要除以2,但是要除以2的话,倒是没有B快)

B. m除以2然后一直加一直到m == n,操作次数是 n-m/2+1

来看一下哪个次数最多,把A的次数减去B的次数,得到

n-m/2 ,因2n > m > n ,所以 n > m/2 > n/2,则 n-m/2 > 0 ,所以,B是最优的方法,也就是说,m > n && m%2 == 0 时,将m除以2。

#include <iostream>
using namespace std;

int main(){
    int n, m, cnt = 0;
    cin >> n >> m;
    if(n >= m) {
	    cout << n-m; return 0;
    }
    while(n != m){
	    if(m < n) m++;
	    else if(m % 2) m++;
	    else m /= 2;
	    cnt++;
    }
    cout << cnt;
    return 0;
}





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
介绍Swing组件的电子书 How This Book Is Organized The book is divided into several chapters, organized by subject: Chapter 1, Basic JComponents Here you'll find simple hacks for the basic components like labels, buttons, and text fields. This chapter contains a lot of bang for the buck, and it illustrates some of the techniques that we will explore more fully later on. From fancy JLabels to translucent menus, this is a great place to start. Chapter 2, Lists and Combos This chapter features complicated Swing components that are used everywhere. Bend them to your will! Make them look good with polymorphic renderers and animated selections. Make them perform well with filtering and Collections support. Chapter 3, Tables and Trees This chapter revelas the secrets of these mystic componentsfrom Excel exporting to proper JTree drop targets. Make the JTree and JTable dance. Chapter 4, File Choosers One of Swing's most maligned components, the JFileChooser, actually has a lot of power hiding inside some murky APIs. This chapter will let you use custom icons, detect Windows shortcuts, and even navigate ZIP files. Chapter 5, Windows, Dialogs, and Frames This is where the fun begins. Every application needs a container, so why not make it pretty and powerful? Make your windows drag and snap. Build custom windows like the earthquake login and spin open dialog. You can even save your window settings automatically with almost no code changes. Chapter 6, Transparent and Animated Windows If you went through the previous chapter and still want more, then this chapter is for you. We push windows to the limit with transparency, animations, slide-in OS X stylesheets, and some of the coolest special effects you've ever seen. Chapter 7, Text Text components seem boring, but there's a lot of power hiding in there. This chapter will show you how to do regular expression searching, dot completion, backward text, and even three different ways to give your application the bright sheen of anti-aliasing. Chapter 8, Rendering This chapter has the meat of the graphics hacks. Custom fonts, a magnifying glass, vector buttons, and even some work with Java3D. We've got some great things to make your application pop. Chapter 9, Drag-and-Drop When your users want two pieces of software to work together the first thing they want to do is drag-and-drop data from their other programs to yours. This chapter covers how to do robust and attractive drag-and-drop entirely within Java. Chapter 10, Audio What would be a cool modern application without some media support? This chapter covers four different ways to play sound, how to display waveforms, and how to embed MP3 support in your own programs. Chapter 11, Native Integration and Packaging The best software works well with the native operating system. Here you'll learn how to launch web browsers, hack the Windows registry, customize your program for specific platforms, and even control iTunes. Chapter 12, Miscellany This chapter offers a grab bag of things that didn't fit anywhere else, but were too cool not to include. Animated cursors, better threading, flashing the keyboard lights, and a bunch of quick one-liners to let you make the most of your busy day.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值