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

精简流,BS下java的效率

Posted by lvhao945 at 2010-02-17 01:01:58 on Problem 3278
import java.util.*;
public class Main
{
	static{
		Scanner in=new Scanner(System.in);
		int a=in.nextInt();
		int b=in.nextInt();
		if(a>=b)System.out.println(a-b);
		else{
			int cnt=0,l=0;
			if(a==0)a=l=1;
			cnt=(int)(Math.log(b/a)/Math.log(2));
			int g=(int)Math.pow(2, cnt);
			cnt+=l;a*=g;
			int h=f(a*2-b,g*2)+1;
			int k=f(b-a,g);
			System.out.println(Math.min(k,h)+cnt);
			}
		System.exit(0);
	}
	static int f(int l,int n)
	{
		if(n==0)return 0;
		int a=l%n,b=a;
	   	l=l/n;
	   	a=f(n-a,n/2)+l+1;
	   	b=f(b,n/2)+l;
	   return Math.min(a, b);
	}
}

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