程序设计基础48 PAT A1033

1033 To Fill or Not to Fill (25 分)

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: C​max​​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D​avg​​ (≤20), the average distance per unit gas that the car can run; and N (≤ 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: P​i​​, the unit gas price, and D​i​​ (≤D), the distance between this station and Hangzhou, for i=1,⋯,N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print The maximum travel distance = X where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

Sample Input 1:

50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300

Sample Output 1:

749.17

Sample Input 2:

50 1300 12 2
7.10 0
7.00 600

Sample Output 2:

The maximum travel distance = 1200.00

一,注意点

1,本题我的卡壳之处在于如何处理到达终点的那段里程,正确的方法是在原有的加油站的基础上加一个价钱为0,距离为到终点距离的加油站。

2,另外我还存在思路不清的问题,实际上本题的关键之处就在于找加油站,两种情况,找到价钱比当前小的加油站(相等不可),或者是所有的价钱比当前加油站大的话找相对最小的那一个。然后对每一种情况分别处理即可。

3,本题我最后纠结的点是为什么若是以后的加油站的价钱都比当前加油站大的话,要在当前加油站加满油,其实是由于距离的原因无法探索到更小的加油站,若想探索到就必须在以后的那些价钱大的加油站加油,能加的少一些就加的少一些。

二,我的代码

#include<cstdio>
#include<algorithm>
using namespace std;
const int INF = 1000000000;
const int max_n = 600;
struct GasStation {
	double price;
	double distance;
}gas_station[max_n];
bool cmp(GasStation A, GasStation B) {
	return A.distance < B.distance;
}
int main() {
	int Cmax = 0, D = 0, Davg = 0, N = 0;
	scanf("%d %d %d %d", &Cmax, &D, &Davg, &N);
	int id_of_station = 0;
	double max_running = Cmax*Davg, now_running = 0;
	double need_oil = 0, now_oil = 0;
	double ans = 0;
	for (int i = 0; i < N; i++) {
		scanf("%lf %lf", &gas_station[i].price, &gas_station[i].distance);
	}
	gas_station[N].price = 0;
	gas_station[N].distance = D;
	sort(gas_station, gas_station + N, cmp);
	if (gas_station[0].distance != 0) {
		printf("The maximum travel distance = 0.00");
		return 0;
	}
	else {
		while (id_of_station < N) {
			int k = -1;
			double min_price = INF;
			for (int i = id_of_station + 1; (i <= N)&&(gas_station[i].distance - gas_station[id_of_station].distance <= max_running);i++) {
				if (gas_station[i].price < min_price) {
					min_price = gas_station[i].price;
					k = i;
					if (min_price < gas_station[id_of_station].price) {
						break;
					}
				}
			}
			if (k == -1)break;
			now_running = gas_station[k].distance;
			if (min_price < gas_station[id_of_station].price) {
				need_oil = (gas_station[k].distance - gas_station[id_of_station].distance) / Davg;
				if (need_oil > now_oil) {
					ans += (need_oil - now_oil)*gas_station[id_of_station].price;
					now_oil = 0;
				}
				else {
					now_oil -= need_oil;
				}
			}
			else {
				need_oil = (gas_station[k].distance - gas_station[id_of_station].distance) / Davg;
				ans += (Cmax - now_oil)*gas_station[id_of_station].price;
				now_oil = Cmax - need_oil;
			}
			id_of_station = k;
		}
		if (id_of_station == N) {
			printf("%.2f", ans);
		}
		else {
			printf("The maximum travel distance = %.2f", now_running + max_running);
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
辽B代驾管理系统对代驾订单管理、用户咨询管理、代驾订单评价管理、代驾订单投诉管理、字典管理、论坛管理、公告管理、新闻信息管理、司机管理、用户管理、管理员管理等进行集中化处理。经过前面自己查阅的网络知识,加上自己在学校课堂上学习的知识,决定开发系统选择小程序模式这种高效率的模式完成系统功能开发。这种模式让操作员基于浏览器的方式进行网站访问,采用的主流的Java语言这种面向对象的语言进行辽B代驾管理系统程序的开发,在数据库的选择上面,选择功能强大的Mysql数据库进行数据的存放操作。辽B代驾管理系统的开发让用户查看代驾订单信息变得容易,让管理员高效管理代驾订单信息。 辽B代驾管理系统具有管理员角色,用户角色,这几个操作权限。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。项目管理页面提供的功能操作有:查看代驾订单,删除代驾订单操作,新增代驾订单操作,修改代驾订单操作。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。新闻管理页面,此页面提供给管理员的功能有:新增新闻,修改新闻,删除新闻。新闻类型管理页面,此页面提供给管理员的功能有:新增新闻类型,修改新闻类型,删除新闻类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值