题目链接: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 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
Author
UESTC
Source
2016 Multi-University Training Contest 6
题目大意:
把音量从p调到q,两种情况:
若p>=q,一直按着每次音量上升1。
若p<q,每次按着的那一秒下降1,接下来一秒下降2,4,8....(2x),x是前一秒下降的值。中途可以选择按键停止或up上升。
解题思路:
两种情况,一种是一直下降,下降到不能下降在上升,另一种是一直下降然后停止在下降。
(需要注意的是由于按up键也可以打断连续向下的功效。所以应该记录停顿了几次,以后向上的时候用停顿补回来)
详见代码。
#include <iostream> #include <cstdio> using namespace std; #define INF 0x3f3f3f3f int s[32],ans; void dfs(int n,int t,int time)//有多少要减掉 休息几次 花了多少时间 { if (n==0) return ; int k=lower_bound(s,s+32,n)-s;//在sum中找到第一个比n(要减掉的数)大的数的下标 int up=s[k]-n;//要向上加多少 /*这里是不休息的,用向上抵掉一部分休息的*/ ans=min(ans,time+k+max(up,t)); /*下面是休息的*/ 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; }