| ||||||||||
| 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 | |||||||||
发一个非bfs的500B版本,不解释//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 );
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator