多校6 1010 HDU5802 Windows 10 dfs

 1 // 多校6 1010 HDU5802 Windows 10
 2 // 题意:从p到q有三种操作,要么往上升只能1步,要么往下降,如果连续往下降就是2^n,
 3 // 中途停顿或者向上,下次再降的时候从1开始。问最少次数
 4 // 思路:
 5 // 1.下么一直往下降,到q的下方,然后再往上升。
 6 // 2.或者往下降到离q最近的一个点再停顿一下 然后继续往下降
 7 // 第一种的时候,上升的时候要减去之前停顿的次数,也就是说我可以提前上升一格,不用停顿。
 8 // 应为上升一格的话,一定是要往上一格
 9 
10 
11 #include <bits/stdc++.h>
12 using namespace std;
13 typedef long long LL;
14 LL dfs(LL p,LL q,LL stop,LL ans){
15     if(p==q) return ans;
16     LL cnt=0;
17     while(p-(1<<cnt)+1>q) cnt++;
18     if(p-(1<<cnt)+1==q) return ans+cnt;
19     LL dis=q-max(0LL,(LL)p-(1<<cnt)+1);
20     LL tem=cnt+max(0LL,dis-stop);
21     return min(tem+ans,dfs(p-(1<<(cnt-1))+1,q,stop+1,ans+cnt));
22 }
23 int main(){
24     int T;
25     scanf("%d",&T);
26     while(T--){
27         LL p,q;
28         scanf("%I64d%I64d",&p,&q);
29         if(p<=q){
30             printf("%I64d
",q-p);
31             continue;
32         }
33         else{
34             LL ans=dfs(p,q,0,0);
35             printf("%I64d
",ans);
36         }
37     }
38     return 0;
39 }
原文地址:https://www.cnblogs.com/ITUPC/p/5749183.html