When the volume decreases, there are two kinds, one is reduced to more than the target m, the other is less than M, the time of the pause can be reduced to the last increase in the number of times, less than 0 when the time becomes 0
Then compare the minimum values for these two types.
1 /*--------------------------------------------------------------------------------------*/2#include <algorithm>3#include <iostream>4#include <cstring>5#include <ctype.h>6#include <cstdlib>7#include <cstdio>8#include <vector>9#include <string>Ten#include <queue> One#include <stack> A#include <cmath> -#include <Set> -#include <map> the - //Debug function for a n*m array - #defineDebug_map (n,m,g) printf ("\ n"); for (int i=0;i< (N); i++) -{ for(intj=0;j< (M); J + +){ +printf"%d", G[i][j]);} printf"\ n");} - //Debug function for int,float,double,etc. + #defineDebug_var (X) cout<< #X "=" <<X<<endl; A #defineLL Long Long at Const intINF =0x3f3f3f3f; - Const intLlinf =0x3f3f3f3f3f3f3f3f; - /*--------------------------------------------------------------------------------------*/ - using namespacestd; - - LL N,m,ans; in intT; - toLL sum (intx) {return(1ll<<x)-1LL;} + -ll DFS (ll Cur,ll CNT,intstop) the { * //printf ("cur:%d cnt:%d\n", cur,cnt); $ if(cur = = M)returnCNT;Panax Notoginseng inti =0; - while(Cur-sum (i) > M) i++; the + if(Cur-sum (i) = = M)returnCNT +i; ALL up = M-max (0ll,cur-sum (i)); theLL res = i + max (0ll,up-stop); + returnMin (Cnt+res, DFS (cur-sum (i-1), CNT + i,stop+1)); - } $ $ intMain () - { -scanf"%d",&T); the while(t--) - {WuyiAns =Llinf; thescanf"%i64d%i64d",&n,&M); - if(N <=M) Wu { -printf"%i64d\n", M-N); About Continue; $ } - -printf"%i64d\n", DFS (N,0,0)); - } A}
Hdu5802-windows 10-dfs+ Greedy