Kattis driversdilemma

Kattis driversdilemma

A car driver visiting Canada from the US travels on a road through isolated New Brunswick territory (no gas stations, houses, or cell phone coverage). Ominous road signs warn about collision risks from deer and moose after dark. Suddenly the driver glances at his fuel gauge with shock: there is exactly half a tank left. He stops and sees that his fuel tank is leaking, which accounts for some of the fuel gauge drop since the last fill up.

The driver cannot repair the leak. Using a container of known volume and his wristwatch, he measures the rate of fuel loss at X gallons per hour. He empties the container back into the tank. He must quickly figure out what to do.

The driver checks the owner’s manual for his rented compact car and finds that the fuel tank capacity is C gallons. He also notices a table resembling (but not necessarily identical to) the one in the figure below, showing declining fuel efficiency in miles per gallon (MPG) as driving speed in miles per hour (MPH) increases. He must trade one against the other in an attempt to reach the nearest gas station M miles away before nightfall.

Since the driver assumes that people who write manuals must be experts, and since he has an aversion to interpolation (and also to extrapolation), he decides that he will drive at exactly one of the 6 speeds listed in the table. That way, the distance he can travel before running out of gas will be 100% predictable.

Can the driver reach the gas station before running out of fuel? If so, at what maximum speed can he drive?

Speed (MPH)Fuel Efficiency (MPG)
5522.0
6021.3
6520.2
7018.3
7516.9
8015.8

Figure 1: Example relationship between speed and fuel efficiency
Input
Input consists of a single test case occupying 7 lines. The first line contains 3 space-separated real numbers: the capacity C of the tank in gallons, the rate of the fuel leak X in gallons per hour, and the distance M in miles to the next gas station, with 0<C,X≤100 and 0<M≤500. Each of the remaining 6 lines contains an integer followed by a real number (separated by a space). The 6 integers at the beginning of these 6 lines are exactly the values in the left column of the table above, in the same order. The corresponding 6 real numbers are in strictly decreasing order, and each such number f satisfies 0<f≤50. Every real value in the input has at most 2 digits after the (optional) decimal point.

Output
If it is possible for the driver to reach the nearest gas station without running out of gas, output “YES” followed by the highest speed at which the driver can drive (separated by a space). This highest speed must be one of the integers 55,60,65,70,75,80. If it is impossible for the driver to reach the nearest gas station before running out of gas, output “NO”. To avoid rounding errors, the following two things are guaranteed: (1) If the driver can reach the gas station, there will be at least 10−6 gallons remaining in the tank when he arrives. (2) If the driver cannot reach the gas station, then he would have required at least 10−6 more gallons of gas in order to be able to do so.

Sample Input 1
18 0.5 160
55 22.0
60 21.3
65 20.2
70 18.3
75 16.9
80 15.8

Sample Output 1
YES 60

Sample Input 2
16 0.07 160
55 20.49
60 18.40
65 17.78
70 17.10
75 16.38
80 15.60

Sample Output 2
NO

本题难度在于读懂题目,只要读懂了题目,就很容易发现此题只是一道简单的物理计算题目,由于题目已经强调将四舍五入改用判断是否满足大于或小于10的负6次方,所以,只需要控制好精度即可。

本题要我们求的是,在满足能跑到下一个加油站的情况下,找到其最大的开车速度。若不能开到加油站,输出NO即可。
我们可以从速度大的数据先使用,看是否满足题意,不满足,则跳到下一个速度小的数据,若都不满足,则不能开到加油站。
首先,我们可以通过表格查出总共要行驶的距离S,通过对应的速度,我们可以先求出从目标点开到加油站的是时间T = S / V。
然后,我们可以通过L = S / 表中每公里油耗 ,算出路上跑车所需要的油耗量。最后,我们再 HT = H * T,算出在T时间内,损失的油耗HT。最终只需要将油箱里的油(题目显示,油箱还剩余一半的油)与公路上损耗的油与跑车时损耗的油相加进行对比,如果油箱中的油减去这些损耗大于10的负6次方,则代表可以到达加油站,反之,此速度无法到达加油站。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
	int flog = 1;
	double L, H, S;
	double a[6][2];
	cin >> L >> H >> S;
	L = L / 2;
	for (int i = 0; i < 6; i++)
		cin >> a[i][0] >> a[i][1];
	for (int i = 5; i >= 0; i--)
	{
		double T, ST, HT, LL;
		LL = L;
		T = S / a[i][0];
		ST = S / a[i][1];
		HT = H * T;
		LL = L - ST - HT;
	//	cout << "*" << T << endl;
	//	cout << "**" << ST << endl;
	//	cout << "***" << HT << endl;
	//	cout << "****" << L << endl;
		if (LL >= 0.000001)
		{
			printf("YES %.0f\n", a[i][0]);
			flog = 0;
			break;
		}
	}
	if (flog == 1)
		printf("NO\n");
		return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值