hdu5802 Windows 10

来源:互联网 发布:win10电脑软件 编辑:程序博客网 时间:2024/06/06 15:37

传送门: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