1870. 准时到达的列车最小时速
题目链接:1870. 准时到达的列车最小时速
代码如下:
class Solution
{
public:
int minSpeedOnTime(vector<int>& dist, double hour)
{
long long h100 = round(hour * 100);
long long delta = h100 - (dist.size() - 1) * 100;
if (delta <= 0)//无法到达终点
{
return -1;
}
int max_dist = ranges::max(dist);
if (h100 <= dist.size() * 100)
{
return max(max_dist, (int)((dist.back() * 100 - 1) / delta + 1));
}
auto check = [&](int v)->bool
{
long long t = 0;
for (int i = 0; i < dist.size() - 1; i++)
{
t += (dist[i] - 1) / v + 1;
}
return (t * v + dist.back()) * 100 <= h100 * v;
};
int h = h100 / (dist.size() * 100);
int left = 0, right = (max_dist - 1) / h + 1;
while (left+1<right)
{
int mid = (left + right) / 2;
(check(mid) ? right : left) = mid;
}
return right;
}
};