Hdu5802-windows 10-dfs+ Greedy

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.