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
贪心。。不知道为什么比赛的时候只有那么一点人过了
对于p>q的时候。考虑减法
如果减完p>q那就一定减,因为倍增,所以减一定比休息一下重置x要划算
如果减完p<=q,我们记录前面一共做的减法论数x,考虑每次减法后都是rest,然后当p<q的时候,这些rest都可以改成add
然后也可以在第一次减法之前先add,比较q-p和前面add的次数,再加上总的减法次数取最小值即可
*当一个数不够减的时候,并不是无法操作,而是会减成0
#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int T;
scanf("%d",&T);
while(T>0)
{
T--;
int p,q;
scanf("%d%d",&p,&q);
if(p<=q)
printf("%d\n",q-p);
else
{
int x=1;
int ans=2100000000,ss=0,sx=0;
while(p>=q)
{
ss++;
p-=x;
x=x*2;
if(p<=q)
{
if(q-p<=sx)
{
ans=min(ans,ss+sx);
break;
}
else
{
//if(p+sx>=0)
ans=min(ans,ss+max(q-max(p,0),sx));
// ans=min(ans,ss+q-p);
ss--;
x=x/2;
p+=x;
sx++;
x=1;
}
}
}
printf("%d\n",ans);
}
}
return 0;
}