HDU 5802 Windows 10【贪心】

Windows 10

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


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
 

Author
UESTC
 

Source
 
思路:
1.p小于q时显然答案是q-p;
2.p>q时,不停地下降到q附近(大于q),分两种情况讨论,第一种是按一次上升,然后再按下降降到小于q(不可能降到0);第二种是接着按下降,直接降到q下(可能出现0),再上升。(降到小于<=0的情况,只出现在第二种情况中,因为如果出现在第一种情况中,解法肯定不如第二种方法按得次数少)。所以这样就能解释的通为什么搜索过程中用上升代替暂停,为什么st(记录按上升键的次数)不影响第二种情况,因为第二种情况下,st==0)。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
#define ll long long
#define ms(a,b)  memset(a,b,sizeof(a))
const int M=1e6+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int i,j,k,n,m;
int sg[M];
int vis[M];
int gcd,x,y;
ll p,q;
ll ans;
ll sum[M];

void dfs(ll cur,ll t,ll st)
{
    if(cur==q){
        ans=min(ans,(ll)t);
        return ;
    }
    int k=1;
    while(cur-sum[k]>q)k++;
    if(cur-sum[k]==q){
        ans=min(ans,(ll)t+k);
        return;
    }
    ll up=max((ll)cur-sum[k],(ll)0);
    ll ups=max((ll)q-up-st,(ll)0);
    ans=min(ans,t+ups+k);
    dfs(cur-sum[k-1],t+k,st+1);
}

int main()
{
    int t;
    scanf("%d",&t);
    ll res=1;
    sum[1]=1;
    for(int i=2;i<=34;i++){
        res*=2;
        sum[i]=sum[i-1]+res;
    }
    while(t--){
        ans=inf;
        scanf("%lld %lld",&p,&q);
        if(p<q)printf("%lld\n",q-p);
        else {
            dfs(p,0,0);
            printf("%lld\n",ans);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值