hdu5802 Windows 10

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5802
这个题就是说从p到q,有三种操作,第一种是加法操作,每次加1,第二种操作是暂停操作,位置不变,第三种是减法操作,第一次减1,第二次减1*2,第三次1*2*2…依次增多。问从p到q最少需要多少步。
就是一个dfs贪心处理一下。

#include<stdio.h>
#include<string>

using namespace std;
typedef long long LL;
LL p,q;
LL judge[40];
void init()
{
    judge[0] = 0;
    for(LL i=1; i<34; i++)
    {
        LL temp = 1LL*1<<(i-(LL)1);
        judge[i] = temp + judge[i-1];
//        printf("%lld\n",judge[i]);
    }
}
LL dfs(LL x,LL step,LL stay)
{
    if(x==q)return step;
    LL now = 0;
    while(x - judge[now] > q)
        now++;
    if(x - judge[now] == q)
        return step + now;
    LL up = q - max((LL)0,x-judge[now]);
    LL temp =  now + max((LL)0,up - stay);
    return min(step + temp,dfs(x - judge[now-1],step+now,stay+1));
}
int main()
{
    int T;
    init();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld %lld",&p,&q);
        if(p<=q)
        {
            printf("%lld\n",q-p);
        }
        else
        {
            LL ans = dfs(p,0,0);
            printf("%lld\n",ans);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值