ACM大一练习赛-第三场-H - 玻色-AlbertEinstein凝聚态《二分查找》
ACM大一练习赛-第三场------H - 玻色-爱因斯坦凝聚态《二分查找》
H - 玻色-爱因斯坦凝聚态
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Description
Now, here is a fuction:
F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.
F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)
Output
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
Sample Input
2 100 200
Sample Output
-74.4291 -178.8534
思路:
因为它这个方程是确定的,题上是让求这个方程的最小值,求导后我们发现,这个倒数是递增的,而且这个方程一开始的倒数是负数,它在导函数为0的时候它能够取到最小值,所以我们要通过二分查找的方法,在x中查找能够使导函数为0的点(因为这个函数是递增函数,所以能够用二分查找),然后将找到的x的值带到函数的方程式里面,就是要求的最小值了!首先不要去盲目的进行二分查找,因为用int型的变量内存是有要求的,所以 首先先将x的范围尽量的缩小,刚开始的时候我也是想将x在0~100的范围内查找有没有使导函数为0的点,谁知道超内存,所以我就去想办法将42*N*N*N*N*N*N+48*N*N*N*N*N+21*N*N+10*N的值取最大的时候比y的最大的值大一点,且不超内存,这样任意的y值都能找到使导函数为0的点,还不会超内存,就能够进行我们一般的做题思路了!
代码:
#include <stdio.h>
#include <string.h>
#include <queue>
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
double y;
double f(double N)
{
return 42*N*N*N*N*N*N+48*N*N*N*N*N+21*N*N+10*N;
}
double erfen(double left,double right)
{
double mid;
while(right-left>1e-7)
{
mid=(right+left)/2;
if(f(mid)-y<1e-7)
{
left=mid;
}
else
{
right=mid;
}
}
return right;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lf",&y);
double t=erfen(0.0,18.0);
double b=6*t*t*t*t*t*t*t+8*t*t*t*t*t*t+7*t*t*t+5*t*t-y*t;
printf("%.4lf\n",b);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。