HDU2899 Strange fuction(三分模板)
HDU2899 Strange fuction
Problem Description
Now, here is a fuction:
F(x) = 6 * x7+8*x6+7x3+5*x2-yx (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
题解
题意
找函数最小值
题解
三分标准例题,整理模板
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
double y = 0;
double cal (double x){
return 6 * pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
}
double tri(){
double midl,midr;
double left=0;
double right=100;
while(left+eps<right){
midl = (left+right)/2;
midr = (midl+right)/2;
double cmidl = cal(midl);
double cmidr = cal(midr);
if(cmidl<cmidr){
right = midr;
}else{
left = midl;
}
}
return left;
}
int main(){
int T = 0;
while(~scanf("%d",&T)){
for(int i=0;i<T;i++){
scanf("%lf",&y);
double ans = tri();
printf("%.4f\n",cal(ans));
}
}
return 0;
}

浙公网安备 33010602011771号