Windows 10
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2179 Accepted Submission(s): 659
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
一开始题意看错了,根本没做。
题意:
从p变到q
1.可以每次上升1。
2.可以休息。
3.可以下降,第一次下降必然是1,然后是2,4,8……,如果上一次是上升或者休息,下降数值变回1。
point:
1.如果p小于q,直接相减。
2.找下降到与q最近的两个点x1,x2。
「
a.休息一次再搜(x1,p)。
b.(p,x2)开始上身,
可以利用休息的时间上升一次,所以最终答案要减去休息的次数。
都要注意,减去的时候最小是0,不能是负数。
如果正好达到p就不能利用休息的次数减,这个要注意。
」
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define ll long long
using namespace std;
ll p,q;
ll sum[35];
ll ans=999999999099999;
void init()
{
ll res=1;
sum[1]=1;
for(int i=2;i<=34;i++)
{
res=res*2;
sum[i]=sum[i-1]+res;
}
}
void dfs(ll now,int step,int stop)
{
if(now==q)
{
ans=min(ans,(ll)step);
return;
}
int k=1;
while(now-sum[k]>q) k++;
if(now-sum[k]==q)
{
ans=min(ans,(ll)step+k);
return;
}
ll up=max((ll)now-sum[k],(ll)0);
ll ups=max((ll)q-up-stop,(ll)0);
ans=min(ans,step+ups+k);
dfs(now-sum[k-1],step+k,stop+1);
}
int main()
{
int T;
scanf("%d",&T);
init();
while(T--)
{
ans=9999999999999999;
scanf("%lld %lld",&p,&q);
if(p<q) printf("%lld\n",q-p);
else
{
dfs(p,0,0);
printf("%lld\n",ans);
}
}
}