Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5802
Windows 10Time
limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total Submission (s): 1005 Accepted Submission (s): 333
Problem Descriptionlong long ago, there is an old monk living on the top of a mountain. Recently, our old monk found the operating system of his computer is updating to Windows automatically and he even can ' t just stop it!!
With a peaceful heart, the old monk gradually accepted this reality because he 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, the voice of his computer can is represented as DB and always be integer.
Because He is old, and 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 the voice, he can press the button, and if the last second he presses the button and the voice decrease X db, then on this second, it'll decrease 2 * x db. If the last second he chooses to has 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 he computer can larger than any dB but can ' t is less tha N 0 DB.
Inputfirst Line contains a number t ( 1≤T ≤300000 ), cases number.
Next T Line,each line contains and numbers p and q (0≤P,q ≤ Ten 9 )
Outputthe minimal seconds he should take
Sample Input
21 57 3
Sample Output
44
Authoruestc
Source2016 multi-university Training Contest 6
Main topic:
Adjust the volume from p to Q, in two cases:
If p>=q, keep pressing the volume up by 1 each time.
If p<q, each time you press the second down 1, the next second drop 2,4,8 .... (2x), X is the descending value of the previous second. You can choose to stop the button or up in the middle.
Problem Solving Ideas:
In both cases, one is always falling down, falling to no drop in the ascent, and the other is falling down and then stopping.
(It is important to note that the continuous downward effect can be interrupted by pressing the UP key.) So it should be recorded a few pauses, after the upward of the time with a pause to fill back )
See the code.
#include <iostream> #include <cstdio>using namespace std; #define INF 0x3f3f3f3fint s[32],ans;void dfs (int n, int t,int time)//How many times it takes to lose a break { if (n==0) return; int K=lower_bound (s,s+32,n)-s;//finds the first subscript int in sum that is larger than n (the number to be lost) up=s[k]-n;//how much to add */* Here is not resting, use upward to offset part of the rest of the * * ans=min (Ans,time+k+max (up,t)); /* Below is the rest of * /if (UP) Dfs (n-s[k-1],t+1,time+k-1);} int main () { s[0]=0; for (int i=1;i<32;i++) { s[i]=2*s[i-1]+1; } int t,p,q; scanf ("%d", &t); while (t--) { scanf ("%d%d", &p,&q); if (p<=q) ans=q-p; else { ans=inf; DFS (p-q,0,0); } int i; for (i=1;i<32;i++) { if (s[i]>=p) break ; } Ans=min (ans,i+q); printf ("%d\n", ans); } return 0;}
HDU 5802 Windows (multi-university Training Contest 6--greedy +dfs)