Windows 10
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
题意:
调节音量s到t,上升音量每秒只能上升1,下降音量每秒为2*x,x为上一次下降的音量,如果下降时休息或者上升音量则x置为1,音量最低为0
分析:
对于s<t的情况,答案则为t-s
对于s>t,采用贪心的策略去解决问题,考虑两种情况:
记stop为下降时休息或者上调音量的次数
1.s下降至小于等于t,那么此时更新答案,答案为当前时间now+(t-s-stop),stop相当于在此处上调了音量,抵消时间
2.s下降至恰不小于t,更新当前时间now,stop++,表示当前有一次休息或者上升音量,继续进行1,2操作循环更新答案
4 4
题意:
调节音量s到t,上升音量每秒只能上升1,下降音量每秒为2*x,x为上一次下降的音量,如果下降时休息或者上升音量则x置为1,音量最低为0
分析:
对于s<t的情况,答案则为t-s
对于s>t,采用贪心的策略去解决问题,考虑两种情况:
记stop为下降时休息或者上调音量的次数
1.s下降至小于等于t,那么此时更新答案,答案为当前时间now+(t-s-stop),stop相当于在此处上调了音量,抵消时间
2.s下降至恰不小于t,更新当前时间now,stop++,表示当前有一次休息或者上升音量,继续进行1,2操作循环更新答案
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
//#pragma comment(linker, "/STACK:1024000000,1024000000");
using namespace std;
#define INF 0x3f3f3f3f
long long dis[1005];
int main()
{
int len=0;
long long x=1;
long long sum=0;
dis[len++]=0;
for(;sum<3000000000;)
{
sum+=x;
dis[len++]=sum;
x*=2;
}
int T;
scanf("%d",&T);
while(T--)
{
long long s,t;
scanf("%lld%lld",&s,&t);
if(s<=t)
{
printf("%d\n",t-s);
continue;
}
int stop=0;
int now=0;
long long ans=INF;
while(1)
{
long long d=s-t;
if(t==s) break;
int pos=lower_bound(dis,dis+len,d)-dis;
long long temp=s-dis[pos];if(temp<0) temp=0;
long long ti=t-temp-stop;if(ti<0) ti=0;
ans=min(ans,pos+now+ti+stop);
if(dis[pos]>d) pos--;
s-=dis[pos];
now+=pos;
if(s==t)
{
ans=min(ans,(long long)now+stop);
break;
}
stop++;
}
printf("%lld\n",ans);
}
}