标签:oid 题解 contest his range ati ota please mode
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2191 Accepted Submission(s): 665
题解:
先尽可能往下降然后升回来,或者尽可能往下降后停然后再往下降,于是就能将问题变成一个子问题,然后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; }
标签:oid 题解 contest his range ati ota please mode
原文地址:http://www.cnblogs.com/stepping/p/7154707.html