Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:发一个非bfs的500B版本,不解释In Reply To:发一个非bfs的500B版本,不解释 Posted by:lzqxh at 2012-05-14 19:11:12 > //By Lin > #include<cstdio> > #include<cstring> > #include<algorithm> > using namespace std; > int ans,n,k,t,g,h; > int main() > { > scanf("%d%d", &n , &k ); > t = 0; ans = abs(n-k); > g = k , h = -1; > while ( g > 1 ) > { > if ( h == -1 ) > if ( g&1 ) h =(g>>1)+1, g >>=1, t +=2; > else g >>=1, t++; > else { > if ( g&1 ) g = h>>1; else g = g>>1; > h = -1; > t++; > } > ans = min( ans , t+abs(n-g) ); > if ( h != -1 ) ans = min( ans , t+abs(n-h) ); > } > printf("%d\n" , ans ); > } 一步步走的时间是N-K,若可以先到K/2处,K为偶则所需时间是N-K/2 + 1,K为奇是 N-K/2 + 2 因为若K是偶数,则到达K的方式是K/2 -> K ;若是奇数,则到达K的方式是K/2 + 1 -> K + 1 -> K(或者 K/2 -> K -1 -> K,取决于K/2是否奇偶,奇则前者,偶则后者); 那么到达K/2也可以采用类似于到达K的策略,如此下去 Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator