链接:戳这里
Windows 10
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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.
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)
Output
The minimal seconds he should take
Sample Input
2
1 5
7 3
Sample Output
4
4
题意:
win10系统声音调节,加声音只能是按一次按钮音量+1,减少的话,假设当前是要减少的,并且上一次也是减少。那么这次是减少上次的两倍。如果上次是加声或者不动。则从1开始减。问P->Q至少需要按钮多少次。
思路:
数据范围小的话bfs+标记可以过,比赛的时候知道会T还是写了一发
正解是贪心。显然如果p<=q,必定只能每次+1才能到达
贪心的理解是:肯定是越往q接近越好,所以要走到当前-2*x比Q小了才去考虑是否休息或者+1或者继续减下去。
考虑p>q:三种状态
1:上次减了x,这次减去2*x
2:rest 休息不动。则x变成1
3:向上调一个音量。x为1
每一次子状态就对应这三种状态,我们考虑合并2、3状态。因为我们可以一直减下去,肯定会减到比Q小,然后加回来
加回来的时候也只能一直+1,而3状态其实也就是向上+1,使得x=1,和2状态是一样的,完全可以把它考虑到后面减到比Q小,再加回来的情况去。这样就把2、3合并了。在后面算比Q小向上加的时候,在减去前面休息了多少次。(这里的休息已经包括了向上+1了。
然后注意一下边界条件,比赛的时候没有分析道2、3合并。。。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
ll DFS(ll P,ll Q,int step,int rest){
if(P==Q) return step;
ll x=0;
while((P-(1LL<<x)+1LL)>Q) x++;
if(max((P-(1LL<<x)+1LL),0LL)==Q) return step+x;
ll up=Q-max(0LL,P-(1LL<<x)+1LL);
ll tmp=max(0LL,up-rest);
return min(tmp+x+step,DFS(P-(1LL<<(x-1))+1,Q,step+x,rest+1));
}
ll n,m;
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%I64d%I64d",&n,&m);
if(n<=m){
printf("%I64d\n",m-n);
continue;
}
printf("%I64d\n",DFS(n,m,0,0));
}
return 0;
}