hdu5802 Windows 10 貪心

來源:互聯網
上載者:User

標籤:ace   output   hidden   algorithm   ios   overflow   end   com   http   

Windows 10

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2096    Accepted Submission(s): 630


Problem DescriptionLong long ago, there was an old monk living on the top of a mountain. Recently, our old monk found the operating system of his computer was updating to windows 10 automatically and he even can‘t just stop it !!
With a peaceful heart, the old monk gradually accepted this reality because his favorite comic LoveLive doesn‘t depend on the OS. Today, like the past day, he opens bilibili and wants to watch it again. But he observes that the voice of his computer can be represented as dB and always be integer.
Because he is old, he always needs 1 second to press a button. He found that if he wants to take up the voice, he only can add 1 dB in each second by pressing the up button. But when he wants to take down the voice, he can press the down button, and if the last second he presses the down button and the voice decrease x dB, then in this second, it will decrease 2 * x dB. But if the last second he chooses to have a rest or press the up button, in this second he can only decrease the voice by 1 dB.
Now, he wonders the minimal seconds he should take to adjust the voice from p dB to q dB. Please be careful, because of some strange reasons, the voice of his computer can larger than any dB but can‘t be less than 0 dB. 

 

InputFirst line contains a number T (1≤T≤300000),cases number.
Next T line,each line contains two numbers p and q (0≤p,q≤109) 

 

OutputThe minimal seconds he should take 

 

Sample Input21 57 3 

 

Sample Output44 

 

AuthorUESTC 

 

Source 2016 Multi-University Training Contest 6
/**題目:hdu5802 Windows 10連結:http://acm.hdu.edu.cn/showproblem.php?pid=5802題意:調節音量p到q,上升音量每秒只能上升1,下降音量每秒為2*x,x為上一次下降的音量,如果下降時休息或者上升音量則x置為1,音量最低為0題意有問題:題目說p不能降為負數。但是如果p-x<0;那麼可以把p-x<0的結果都看做p-x==0;思路:貪心的去選,每次下降到終點上的最近一點,或者終點下的一點,取得一個最小值就好。*/#include <iostream>#include <cstdio>#include <vector>#include <cstring>#include <cmath>#include <algorithm>using namespace std;typedef long long LL;const int mod=1e9+7;const int maxn=1e6+5;const double eps = 1e-12;LL dfs(LL p,LL q,LL delay){    LL x = 1;    LL cnt = 0;    while(p-x>=q){        cnt++;        p = p-x;        x = x*2;    }    if(p==q) return cnt;    return min(max(0LL,q-max(0LL,(p-x))-delay)+cnt+1,dfs(p,q,delay+1)+cnt+1);}int main(){    int T;    LL p, q;    cin>>T;    while(T--)    {        scanf("%lld%lld",&p,&q);        if(p<q){            printf("%lld\n",q-p);        }else        {            printf("%lld\n",dfs(p,q,0));        }    }    return 0;}

 

hdu5802 Windows 10 貪心

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.