HDU 5802 Windows 10 (贪心+dfs)

Windows 10

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5802

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 (1≤T≤300000),cases number.
Next T line,each line contains two numbers p and q (0≤p,q≤109)

Output


The minimal seconds he should take

Sample Input


2
1 5
7 3

Sample Output


4
4

Source


2016 Multi-University Training Contest 6


题意:


要把音量从p调节到q. 每次调节需要1秒:

  1. 按up会+1.
  2. 按down:若上一秒是休息/up则-1;若上一秒下降x则-2*x.


题解:


比赛的时候没有想清楚,看了题解后才知道贪心就可以了.
若p<=q肯定一直按up就可以了.
若 p>q , 贪心的想法是一直按down(中间可能停顿)直到p<=q,然后再按up升回来.
需要注意的一点是,停顿是为了打断连续的down下降太快,而按up也有打断的功效,所以用最后的up操作来抵消停顿(相当于提前按up).


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 150
#define mod 110119
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

LL s, t;

LL dfs(LL cur, LL step, LL stop) {
    if(cur == t) return step;
    LL down = 0;
    while(cur - (1<<down) + 1 > t) down++;
    step += down;
    if(cur - (1<<down) + 1 == t) return step;

    LL up = t - max(0LL, cur - (1<<down) + 1);

    return min(step+max(up-stop,0LL), dfs(cur-(1<<(down-1))+1, step, stop+1));
}

int main(int argc, char const *argv[])
{
    //IN;

    int T; cin >> T;
    while(T--)
    {
        cin >> s >> t;

        LL ans = 0;
        if(s <= t) ans = t - s;
        else ans = dfs(s, 0, 0);

        printf("%lld\n", ans);
    }

    return 0;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5741018.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值