標籤:oid 題解 contest his range ati ota please mode
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 DescriptionLong 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.
InputFirst 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)
OutputThe minimal seconds he should take
Sample Input21 57 3
Sample Output44
AuthorUESTC Source2016 Multi-University Training Contest 6 Recommendwange2014
題解:
先儘可能往下降然後升回來,或者儘可能往下降後停然後再往下降,於是就能將問題變成一個子問題,然後dfs就好,需要注意的是由於升也可以打斷連續降的功效,所以應該記錄停頓了幾次,以後上升的時候先用停頓補回來,不夠再接著升,時間複雜度O(Tlogq)
#include <bits/stdc++.h>using namespace std;const int inf=0x7fffffff;typedef long long ll;ll res;int T;long long p,q;long long sum[50];void dfs(ll x,ll y,ll ti,ll stop){ if (x==y) {res=min(res,ti); return;} int k=1; while(x-sum[k]>y) k++; if (x-sum[k]==y) { res=min(res,ti+k); return;} ll up=q-max((ll)0,x-sum[k]); res=min(res,ti+k+max((ll)0,up-stop)); //停頓可以替換向上按 dfs(x-sum[k-1],y,ti+k,stop+1); //停頓次數+1,向下減音量從1開始 return;}int main(){ for(int i=1;i<=31;i++) sum[i]=(1<<i)-1; scanf("%d",&T); for(;T>0;T--) { scanf("%lld%lld",&p,&q); if (p<=q) printf("%lld\n",q-p); else { res=inf; dfs(p,q,0,0); printf("%lld\n",res); } } return 0;}
hdu 5802 Windows 10 (dfs)