ZOJ Problem Set - 2767
88 is the nickname of freecity, the most famous bbs in ZJU. It has so many user, and the number of articles in all boards are getting more and more every minute. It was hard to find an article even when you knew its index. But now, 88 has a new function, with which you can goto the article you want immediately after inputing its index. For example , when you want to goto the article at with index 75 , you can simply press the key '7' and '5' and at last the 'Enter' key, then you'll see you cursor before the 75th article. You can also use 'up' and 'down' key to go up or down one article respectively. If you want to goto '76', you can press '7' ,'5', 'Enter','down' four keys sequentially. (Though it is not a good way to goto '76', here it is just for explaining the function) 68 M swinge Jul 28 . missing you tonight.
69 M CoolBug Jul 29 Re: ec last night
Topiori likes surfing on 88, and spends so much time on it that it is a great part of his daily life. But his keyboard has met some problems recently - some number keys do not work, while he still want to use it to surfing on 88. Now he is asking you for help: he wants to use the keyboard as least as possible to go to the correct article he's interesting in. He believe a good programmer like you can easily find the best way.
*Be a little difference from the real 88 operate, can't press 'down' key to go from the last article to the first one, also can't press 'up' key to go from the first one to the last.
There are multiple test cases.
With every case you just needed to output one line with one integer K, the least times of key pressing.
(Hint: '7', '4', 'Enter', 'down', or '7', '6', 'Enter', 'up')
Author: YANG, Xuan
Source: ZOJ Monthly, October 2006