hdu 5802 Windows 10

题目:

Windows 10

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


Problem Description
Long 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.
 

Input
First line contains a number T ( 1T300000 ),cases number.
Next T line,each line contains two numbers p and q (0p,q109)
 

Output
The minimal seconds he should take
 

Sample Input
  
  
2 1 5 7 3
 

Sample Output
  
  
4 4


题意:你有一个收音机,但是按钮有问题。一秒钟只能按一次按钮,当你往上按的时候,音量加一,当你往下按得时候:如果你上一秒也是往下按,那么就减少上一秒的双倍音量,否则就减少。问:从音量p到音量q最少要几秒;


思路:

         明显p<=q时,结果为q-p。

         p>q时,结果要么一直按向下键到现在的音量小于q然后在往上增加,或者是一直向下到最接近q但是大于q的音量now(因为是下降值2的倍数,所以先下降足够多的抉择一定优于或等于其他下降但不小于q的抉择),然后在递归操作从now到q;

         易错点 1.下降的最低点为0,也就是说当要出现负数的时候置为零。

                     2.由于在每次递归操作过程中,要休息或向上走一次(这样不会受到之前下降值的印象)。这个时候休息一次和向上一次(第二或更多次向上没有必要,因为在音效小于q后也可以向上)的虽然用时一样,但是所在的音量差一。此处要特判

       

#include <bits/stdc++.h>

using namespace std;
long long num[40];
int dfs(int des,int now,int mayup)
{
    if((now-des)==0)
    {
        return 0;
    }
    int x=now-des;
    int len=lower_bound(num,num+32,x)-num;
    int l,r;
    if(num[len]==x||num[len]-mayup<=x)    //特判 mayup为可能存在的向上次数
    {
        return len;
    }
    else
    {
        l=num[len-1];
        r=num[len];
    }
    int min1;
    if(now+mayup<=r)
        min1=0;
    else
        min1=mayup+now-r;
    return len+min(dfs(des,now-l,mayup+1),des-min1);
}
int main()
{
    int t,p,q;
    for(int i=1;i<=31;i++)
    {
        num[i]=(num[i-1]+1)*2-1;
    }
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&p,&q);
        if(p<=q)
        {
            cout<<(q-p)<<endl;
        }
        else
        {
            cout<<dfs(q,p,0)<<endl;
        }
    }
    return 0;
}







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值