甲级PAT 1033 To Fill or Not to Fill (贪心)

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

题目要求:

一辆车,其油箱的最大容积为Cmax(假设单位为升),某人要开这辆车从A前往B,A与B之间的距离为D km,这辆车平均每消耗1升油可行驶D​avg km,在A与B的路上有N个加油站,各加油站在不同的位置,其油价可能不相同。在A时油箱油量为0,如果能从A到B,求最小的花费;若不能,则计算出最大行驶距离。

解题思路:

这里首先将各加油站按照距离进行排序,如果没有距离为0的加油站,则直接输出The maximum travel distance = 0.00;若有则进行下面的判断。

起点的加油站会有一个油价,那么该加多少最为合适?这里要用到贪心的策略。

如果在能到达的最大范围内,有加油站的油价比当前的油价低,那肯定加到能够恰好到这个油价比当前低的加油站距离的油量就是一个局部最优策略(这个并不一定是范围内加油站最小的油价)。

如果在最大的范围内没有加油站油价比当前低,那就加满(加满是因为,无论在范围之外有多小多小的油价,即使加满也无法到达,还需要中间一个加油站中转,但既然当前油价最低,能加满就加满,之后油钱高的就可以少加点),下一个到达的加油站因为在范围内油价最小的加油站(即使最小也比当前油价高)

不断的循环这个过程,在正常情况下到达目的地就终止循环,根据上述策略,可以将目的地也看做加油站,其距离设置为相应距离,油钱为0,这样其油钱一定比所有的加油站油钱都要低,在最后一定会选择目的地作为下一到达加油站。但是要注意,有可能当前加油站最近的一个加油站超过了车能行驶的最大距离(即最大的油量*每升行驶路程),则无法到达目的地,最长距离就是当前加油站的位置+车能行驶的最大距离。

注意:

1.在油箱加满后到达另一个加油站,要考虑油箱剩余油量。即每次油箱加满之后,到达下一加油站的实际剩余油量为cmax - (station[min].distance - station[now].distance)/avg

2.在计算加满花费时,也要考虑油箱剩余油量。这部分是不算的,之前直接用cmax*station[now].price,最后的价钱变多。

完整代码:

#include<bits/stdc++.h>
using namespace std;

struct Station{
	double distance;
	double price;
}station[510];

double cmax,D,avg;

bool comp(Station s1,Station s2){
	if(s1.distance < s2.distance) return true;
	else if(s1.distance == s2.distance) return s1.price<s2.price;
	else return false;
}

int main(){
	int N,i,min,now;
	double cost=0,c;
	cin>>cmax>>D>>avg>>N;
	for(i=0;i<N;i++){
		scanf("%lf%lf",&station[i].price,&station[i].distance);
	}
	station[N].distance = D;
	station[N].price = 0;
	sort(station,station+N+1,comp);
	if(station[0].distance !=0){
		cout<<"The maximum travel distance = 0.00";
	}else{
		while(now<N){
			min = now;
			if(station[now].distance+cmax*avg < station[now+1].distance){
				printf("The maximum travel distance = %.2f",station[now].distance+cmax*avg);
				break;
			}
			for(i=now+1;i<=N;i++){
				if(station[now].distance+cmax*avg < station[i].distance) break;
				if(station[i].price <= station[now].price){
					min = i;
					break;
				}
			}
			if(min == now){
				min = now +1; 
				for(i=now+2;i<=N;i++){
					if(station[now].distance+cmax*avg < station[i].distance) break;
					if(station[i].price <= station[min].price){
						min = i;
					}
				}
				cost += (cmax-c)*station[now].price;
				c = cmax - (station[min].distance - station[now].distance)/avg;
				now = min;
			}else{
				c = (station[min].distance - station[now].distance)/avg -c;
				cost += c*station[now].price;
				c = 0;
				now = min;
				
			}
		}
		if(now == N){
			printf("%.2f",cost);
		}
	}
}

 

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值