Windows 10
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 471 Accepted Submission(s): 111
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
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<bitset>
#define pi acos(-1.0)
#define inf 1<<29
#define INF 0x3f3f3f3f
#define zero 1e-8
const int li[]={-1,0,1,0};
const int lj[]={0,-1,0,1};
using namespace std;
int n,m;
int greedy(){
int tem=0;
int ans=inf;
int now=n;
int cnt=-1;
while (n<m){
int k;
for (int i=0;i<=30;++i){
k=1<<i;
if (k-1>m-n) {
tem+=i-1;
cnt++;
break;
}
now=k-1;
}
int fk=(m-now-(now+1))<0?0:(m-now-(now+1)),tm;
m-=now;
if (m!=n) tem++;
if (n-fk<cnt) tm=0;
else tm=n-fk-cnt;
ans=ans>(tem+tm)?(tem+tm):ans;
}
ans=ans>tem?tem:ans;
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while (t--){
scanf("%d %d",&m,&n);
if (m<=n) printf("%d\n",n-m);
else {
printf("%d\n",greedy());
}
}
return 0;
}