Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

发一个非bfs的500B版本,不解释

Posted by lzqxh at 2012-05-14 19:11:12 on Problem 3278
//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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator