设加油时间为 t,那么总用时 = t + y / (v0 + xt) = t + (y / x) / (v0 / x + t)
令 c = y / x,a = v0 / x,那么原式 = t + c / ( t + a) = (t + a) + c / (t + a) - a <= 2 * sqrt(c) - a。
根据基本不等式,我们知道两个数的算术平均值小于等于两个数的几何平均值,当且仅当两者相等时,才取等号。
注意这里有一个隐含条件,t >=0。那么a的平方大于c时,取不到这个最小值。不过没关系,此时原式随着t的增加单调递增,因此t = 0时就是最小值。
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
int v0, x, y;
cin >> v0 >> x >> y;
double c = (double)y / x;
double a = (double)v0 / x;
if(a * a <= c) {
cout << fixed << setprecision(10) << sqrt(c) * 2 - a;
} else {
cout << fixed << setprecision(10) << (double)y / v0;
}
}
// 64 位输出请用 printf("%lld")



京公网安备 11010502036488号