HDU 5802 Windows 10(dfs)

这题告诉你从p到q,往上走只能+1,往下是1,2,4,8。。。这样
可以停顿,凡是停顿和+1,都会打断往下走的等比数列,问你最少多少步到达q。


如果p在下面,那么只能往上走,如果p在上面的话,题解说只有两种情况,一种是往下走,然后往上走,一种是往下走,然后停顿,然后往下走。
所以可以分成两个问题,当前,是走到底,然后网上走更优,还是停顿,然后再往下走。
对于最后的往上走,如果前面有停顿,可以把前面的停顿换成往上走,这样就可以减少步数。
感觉这题挺玄的真心没想到,居然就是这样两种情况,要么往下走,要么就停顿,然后停顿可以在最后换成往上走。
这样搜索的时候记录走了多少步,停顿了多少次就行了。


代码:

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")

using namespace std;
#define   MAX           200005
#define   MAXN          1000005
#define   maxnode       205
#define   sigma_size    26
#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   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   pii           pair<int,int>
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair
#define   limit         10000

//const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 0x3f3f;
const double pi    = acos(-1.0);
const double inf   = 1e18;
const double eps   = 1e-4;
const LL    mod    = 1e9+7;
const ull    mx    = 133333331;

/*****************************************************/
inline void RI(int &x) {
      char c;
      while((c=getchar())<'0' || c>'9');
      x=c-'0';
      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
 }
/*****************************************************/

LL dfs(LL p,LL q,LL num,LL stop){
    int x=0;
    while(p-(1<<x)+1>q) x++;
    if(p-(1<<x)+1==q) return x+num;
    LL a1=q-max(0LL,p-(1<<x)+1);
    LL a2=x+max(0LL,a1-stop);
    return min(a2+num,dfs(p-(1<<(x-1))+1,q,num+x,stop+1));
}
int main(){
    //freopen("in.txt","r",stdin);
    int t;
    cin>>t;
    while(t--){
        int p,q;
        scanf("%d%d",&p,&q);
        if(p<=q){
            printf("%d\n",q-p);
            continue;
        }
        printf("%I64d\n",dfs(p,q,0,0));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值