zoukankan      html  css  js  c++  java
  • hdu-5802 Windows 10(贪心)

    题目链接:

    Windows 10

    Time Limit: 2000/1000 MS (Java/Others)  

      Memory Limit: 65536/65536 K (Java/Others)


    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的最短时间是多少?
     
    思路:
     
    看的别人的才知道怎么做,每次连续下降x次,一共下降2x-1的音量;每次下降到比q大或者比q小最接近q的数,为什么这样呢?
    假设有两次的都连续下降x次.那么可以连成一个x+1的的下降,所以前边连续下降的次数比后边大;
     
    AC代码:
     
    /************************************************
    ┆  ┏┓   ┏┓ ┆   
    ┆┏┛┻━━━┛┻┓ ┆
    ┆┃       ┃ ┆
    ┆┃   ━   ┃ ┆
    ┆┃ ┳┛ ┗┳ ┃ ┆
    ┆┃       ┃ ┆ 
    ┆┃   ┻   ┃ ┆
    ┆┗━┓    ┏━┛ ┆
    ┆  ┃    ┃  ┆      
    ┆  ┃    ┗━━━┓ ┆
    ┆  ┃  AC代马   ┣┓┆
    ┆  ┃           ┏┛┆
    ┆  ┗┓┓┏━┳┓┏┛ ┆
    ┆   ┃┫┫ ┃┫┫ ┆
    ┆   ┗┻┛ ┗┻┛ ┆      
    ************************************************ */ 
     
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    //#include <bits/stdc++.h>
    #include <stack>
    #include <map>
     
    using namespace std;
     
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
     
    typedef  long long LL;
     
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
     
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const LL inf=1e18;
    const int N=2e5+10;
    const int maxn=2e3+14;
    const double eps=1e-12;
    
    LL ans,f[40];
    void dfs(LL p,LL q,LL time,int num)
    {
        if(max(p,0LL)==q)
        {
            ans=min(ans,time);
            return ;
        }
        else if(max(p,0LL)<q)
        {
            LL temp=q-max(0LL,p);
            ans=min(ans,time+max(temp-num,0LL));
            return ;
        }
        int l=1;
        while(p-(f[l]-1)>q)l++;
        dfs(p-(f[l]-1),q,time+l,num);
        if(l>1)dfs(p-(f[l-1]-1),q,time+l,num+1);
    }
    
    inline void solve(LL p,LL q)
    {
        ans=inf;
        dfs(p,q,0,0);
        printf("%lld
    ",ans);
    }
    inline void Init()
    {
        f[0]=1;
        For(i,1,35)f[i]=f[i-1]*2;
    }
    int main()
    {
        int t;
        LL p,q;
        read(t);
        Init();
        while(t--)
        {
            read(p);read(q);
            solve(p,q);
        }
        return 0;
    }
    

      

  • 相关阅读:
    洛谷 1341 无序字母对
    POJ 2774 后缀数组 || 二分+哈希
    HDU 1251 统计难题
    【解题报告】AtCoder ABC115 (附英文题目)
    【模板】后缀数组
    洛谷 3567/BZOJ 3524 Couriers
    Beta 冲刺 (1/7)
    团队项目评测
    beta冲刺前准备
    Alpha冲刺——事后诸葛亮
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5746509.html
Copyright © 2011-2022 走看看