hdu 5802——Windows 10

题意:给定两个音量,需要从l调到r,每次只能向上一下或者向下(1,2,4,2^n)下(连续向下的情况下),每次可以停歇或向上来打断向下的连续性,最少需要多少步?
思路:当l小于r时显然答案是r-l,当l>r时那么不停地下降到r附近,此时只需要在r的上方和下方挑选最少的步数即可。注意向上也可以打断向下的连续性。
code:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int INF=0x3fffffff;
const int inf=-INF;
const int N=105;
const int M=2005;
const int mod=1000000007;
const double pi=acos(-1.0);

#define cls(x,c) memset(x,c,sizeof(x))
#define cpy(x,a) memcpy(x,a,sizeof(a))
#define ft(i,s,n) for (int i=s;i<=n;i++)
#define frt(i,s,n) for (int i=s;i>=n;i--)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt  rt<<1
#define rrt  rt<<1|1
#define middle int m=(r+l)>>1
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define mk make_pair
#define IN freopen("in.txt","r",stdin)
#define OUT freopen("out.txt","w",stdout)

int read() {
    char ch;
    while (ch = getchar(), !isdigit(ch));
    int res = ch - '0';
    while (ch = getchar(), isdigit(ch))
        res = res * 10 + ch - '0';
    return res;
}
ll powm(ll a,ll n){
    ll ans=1;
    while (n){
        if (n&1) ans=ans*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return ans%mod;
}
//++++++++++++密++++++++++++++封++++++++++++++++++++线

ll n,m;
ll sol(ll n,ll stp){
    if (n<=m) return max(0LL,m-n-stp);
    ll nn=n,p=1,sp=0;
    while (n-m>0){
        n-=p;
        p<<=1;
        sp++;
        if (n==m) return sp;
    }
    sp+=min(sol(n+p/2,++stp),sol(max(0LL,n),stp));
    return sp;
}
int main()
{
    int T=read();
    ft(ca,1,T){
        scanf("%lld%lld",&n,&m);
        cout<<sol(n,0)<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值