HDU5802

题意:

win10系统声音调节,加声音只能是按一次按钮音量+1,减少的话,假设当前是要减少的,并且上一次也是减少。那么这次是减少上次的两倍。如果上次是加声或者不动。则从1开始减。问P到Q至少需要按钮多少次。

每一种状态可以分成三种子状态

1:上次减了x,这次减去2*x

2:rest 休息不动。则x变成1

3:向上调一个音量。x为1

对于向上显然是为了凑足能够连续向下的数,那么一直向下到q以下再上升是一样的,就可以不用考虑这种情况,递归值须递归两种子状态就简单很多,但是要注意当最后一次如果越过q到负数那么考虑成到0。还有一个细节就是,当在q以下假设需要向上升的步数为q1,到目前为止休息的次数为sto,那么向上升的步数可以考虑成max(q1,sto)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int N = 50000+10;
const int mod=1e9+7;
const double en=2.718281828459;
using namespace std;
int t;
ll p,q;
ll dfs(ll p,ll q,ll ste,ll sto){
  int  x=0;
  while(p-(1<<x)+1>q) x++;
// cout<<x<<" "<<ste<<endl;;
  if(p-(1<<x)+1==q) return x+ste;
  ll up;
  up=q-max((ll)0,p-(1<<x)+1);
  ll ans=max((ll)0,up-sto);
  return min((ll)(x+ans+ste),dfs(p-(1<<(x-1))+1,q,ste+x,sto+1));
}
int main() {
 //freopen("in.txt","r",stdin);
 cin>>t;
 while(t--){
  scanf("%lld%lld",&p,&q);
  if(p<=q) printf("%d
",q-p);
  else printf("%lld
",dfs(p,q,0,0));

 }
}