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

AC了..贪心逼近 O(N) OMS..具体方法如下

Posted by humanjustic at 2006-12-05 21:11:16 on Problem 3039
方法是取两个指针 f,l : f 始终表示前面的数,做分子;而 l 始终是表示后面的数,做分母.
初始化 f=l=1;
所以在更新最优解之后:

如果 f/l<(输入) ,如果r++的话,只会使 f/l 更小,说明只能移动l指针,更趋于最优解,故此时f+=1;

如果 f/l>(输入),如果l++的话,只会使 f/l 更大,更离最优值远去,说明只能r++逐步接近最优解,故此时l+=1。

同时在逼近过程不断更新最优值并记录即可..

核心代码:
f=1;l=1;tmp=INF;
		
while(f<=Maxsize&&l<=Maxsize){

         if( fabs((double)f*1.0/(double)l*1.0-res) < tmp && (f*b!=l*a)){
		tmp=fabs((double)f*1.0/(double)l*1.0-res);
		aa=f;bb=l;//aa,bb分别记录最优值
	}

	if( (double)f*1.0/(double)l*1.0< res )
		f+=1;
	else
		l+=1;
}

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