2016暑假多校联合---Windows 10
2016暑假多校联合---Windows 10(HDU:5802)
Problem Description
Long long ago, there was an old monk living on the top of a mountain. Recently, our old monk found the operating system of his computer was updating to windows 10 automatically and he even can't just stop it !!
With a peaceful heart, the old monk gradually accepted this reality because his favorite comic LoveLive doesn't depend on the OS. Today, like the past day, he opens bilibili and wants to watch it again. But he observes that the voice of his computer can be represented as dB and always be integer.
Because he is old, he always needs 1 second to press a button. He found that if he wants to take up the voice, he only can add 1 dB in each second by pressing the up button. But when he wants to take down the voice, he can press the down button, and if the last second he presses the down button and the voice decrease x dB, then in this second, it will decrease 2 * x dB. But if the last second he chooses to have a rest or press the up button, in this second he can only decrease the voice by 1 dB.
Now, he wonders the minimal seconds he should take to adjust the voice from p dB to q dB. Please be careful, because of some strange reasons, the voice of his computer can larger than any dB but can't be less than 0 dB.
With a peaceful heart, the old monk gradually accepted this reality because his favorite comic LoveLive doesn't depend on the OS. Today, like the past day, he opens bilibili and wants to watch it again. But he observes that the voice of his computer can be represented as dB and always be integer.
Because he is old, he always needs 1 second to press a button. He found that if he wants to take up the voice, he only can add 1 dB in each second by pressing the up button. But when he wants to take down the voice, he can press the down button, and if the last second he presses the down button and the voice decrease x dB, then in this second, it will decrease 2 * x dB. But if the last second he chooses to have a rest or press the up button, in this second he can only decrease the voice by 1 dB.
Now, he wonders the minimal seconds he should take to adjust the voice from p dB to q dB. Please be careful, because of some strange reasons, the voice of his computer can larger than any dB but can't be less than 0 dB.
Input
First line contains a number T (1≤T≤300000),cases number.
Next T line,each line contains two numbers p and q (0≤p,q≤109)
Next T line,each line contains two numbers p and q (0≤p,q≤109)
Output
The minimal seconds he should take
Sample Input
2
1 5
7 3
Sample Output
4
4
Author
UESTC
Source
Recommend
题意:有一个收音机,音量从0~无穷大,有音量调大和调小两个键,若按调大键,每次音量加一,如按调小键,第一次减1,第二次减2,第三次减4……倍数增长,每秒只能按一次键,若上一秒没按键或者是调大键,这一秒按的是调小键,则音量减一,求从a调到b的最少的时间;
思路:若a<=b,则结果就是b-a;若a>b,dfs分治的思想,每次尽可能向下减音量;
代码如下:
#include <iostream> #include <stdio.h> #include <algorithm> using namespace std; typedef long long ll; ll sum[50]; ll ans,a,b; void init() { sum[0]=0; for(ll i=1; i<=33; i++) sum[i]=(1<<i)-1; } ll dfs(ll x,ll step,ll stop) { if(x==b)return step; ///x 当前位置,等于b 时退出当前栈 int k=0; while(x-sum[k]>b) //到k值,向下跳k步后 使得当前位置小于等于b位置 k++; if(x-sum[k]==b) return step+k; ///刚好跳到b位置 ll up =b-max((ll)0,x-sum[k]);///x-sum[k] 在b下面 --> 向上跳的步数并且最多走到0位置 ll res=k+max((ll)0,up-stop); ///加入走了k步,再往上走,总共 k+up-stop 步 ///up - stop ,往上走就不需要停顿了,up的步数比停顿的多 用up 顶替停顿, return min(step+res,dfs(x-sum[k-1],step+k,stop+1));///取现在向上反 和继续向下跑的最小的那个 } int main() { init(); int t; scanf("%d",&t); while(t--) { scanf("%lld%lld",&a,&b); if(a<=b) { printf("%lld\n",b-a); continue; } else printf("%lld\n",dfs(a,0,0)); } return 0; }