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 (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

题意:

问从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(‘\n‘);
}

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\n",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;
}

  

时间: 2024-08-25 10:20:27

hdu-5802 Windows 10(贪心)的相关文章

hdu 5802 Windows 10(2016 Multi-University Training Contest 6——贪心+dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5802 Windows 10 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1005    Accepted Submission(s): 333 Problem Description Long long ago, there was a

hdu 5802 Windows 10

官方题解: Windows 10 _您可能是正版Windows 10的受害者_ 直接贪心就好 比较直观的看法是使劲往下降,然后升回来 或者使劲往下降然后停顿然后再使劲往下降... 于是就能将问题变成一个子问题,然后dfs就好 需要注意的是由于按up键也可以打断连续向下的功效 所以应该记录停顿了几次,以后向上的时候用停顿补回来 /*by*/ #include <iostream> #include <algorithm> #include <cstdio> #includ

hdu 5802 Windows 10 (dfs)

Windows 10 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2191    Accepted Submission(s): 665 Problem Description Long long ago, there was an old monk living on the top of a mountain. Recently,

hdu5802 Windows 10 贪心

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 Description Long long ago, there was an old monk living on the top of a mountain. Recently,

2016暑假多校联合---Windows 10

2016暑假多校联合---Windows 10(HDU:5802) 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 j

hdu 4296 Buildings(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4296 Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1822    Accepted Submission(s): 722 Problem Description Have you ever heard the sto

HDU 4903 (模拟+贪心)

Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a heat for years in China. The game goes with the 54 poker cards for 3 players, where the “Landlord” has 20 cards and the other two (the “Farmers”) have 1

HDU 1338 Game Prediction 贪心

Problem Description Suppose there are M people, including you, playing a special card game. At the beginning, each player receives N cards. The pip of a card is a positive integer which is at most N*M. And there are no two cards with the same pip. Du

hdu 1009 FatMouse&#39; Trade(贪心)

题目来源:hdu 1009 FatMouse' Trade FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 54581 Accepted Submission(s): 18299 Problem Description FatMouse prepared M pounds of cat food, ready