HDU5802-windows 10-dfs+贪心

时间:2023-03-10 05:44:53
HDU5802-windows 10-dfs+贪心

音量减的时候,分两种,一种是减到大于目标M,另一种是减到小于M,停顿的时候可以减少最后往上加的次数,小于0的时候变成0

然后比一下这两种的最小值。

 /*--------------------------------------------------------------------------------------*/
#include <algorithm>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map> //debug function for a N*M array
#define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\
{for(int j=;j<(M);j++){\
printf("%d",G[i][j]);}printf("\n");}
//debug function for int,float,double,etc.
#define debug_var(X) cout<<#X"="<<X<<endl;
#define LL long long
const int INF = 0x3f3f3f3f;
const int LLINF = 0x3f3f3f3f3f3f3f3f;
/*--------------------------------------------------------------------------------------*/
using namespace std; LL N,M,ans;
int T; LL sum(int x) {return (1LL<<x) - 1LL;} LL dfs(LL cur,LL cnt,int stop)
{
//printf("cur:%d cnt:%d\n",cur,cnt);
if(cur == M) return cnt;
int i = ;
while(cur-sum(i) > M) i++; if(cur -sum(i) == M ) return cnt + i;
LL up = M - max(0LL,cur-sum(i) );
LL res = i + max(0LL,up-stop);
return min(cnt+res , dfs(cur-sum(i-) , cnt + i,stop+));
} int main()
{
scanf("%d",&T);
while(T--)
{
ans = LLINF;
scanf("%I64d%I64d",&N,&M);
if(N <= M)
{
printf("%I64d\n",M-N);
continue;
} printf("%I64d\n",dfs(N,,));
}
}