PAT A1033 To Fill or Not to Fill

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
  • 分析:
    两个需求:
    case 1:到达目的地,加的油钱最少
    case 2:如果到不了目的地,能走的最远距离

  • 坑点:
    Wrong 1: 出发时油箱是空的,如果没有distance == 0 的加油站,就走不了,需要判特
    (样例3)

  • 思路:贪心
    这题还是挺麻烦的,先分析样例:
    样例的走法是:
    在这里插入图片描述0->1:油箱0 + 150km的油
    1->3:油箱0 + 150km
    3->5:0 + 600km
    5->6: 300 + 300km
    6->7: 200 + 50 km

观察可发现:每走到一个加油站有两种选择:
从这个加油站(now)出发,(在可到范围内:满油最远距离内):
case 1:如果有比now油钱便宜的加油站next,下一个加油站就是next,且本次只加到够到next的油(有两种情况:油不够、油够),这样能在next以后将now的贵油换成便宜些的油:鸟枪换炮
case 2:如果没有比now便宜的,相对来说最便宜的加油站(Min),本次加满油,因为这一轮里now的油价最低(过了这村没这店了),及时跑到最远的加油站也不可能鸟枪换炮了,只能加满油到下一个落脚点(相对便宜的加油站)开始从长计议

  • code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1100;

struct Station{
	double price, distance;
}sta[maxn];
bool cmp(Station a, Station b){
	return a.distance < b.distance;
}
int main(){
	int n;
	double capacity, sum_distance, d_avg;
	scanf("%lf %lf %lf %d", &capacity, &sum_distance, &d_avg, &n);
	for(int i = 0; i < n; ++i){
		scanf("%lf %lf", &sta[i].price, &sta[i].distance); 
	}
	sta[n].distance = sum_distance; sta[n].price = 0;
	sort(sta, sta+n, cmp);
	if(sta[0].distance != 0){	//Wrong1:样例3,杭州不一定有加油站... 
		printf("The maximum travel distance = 0.00\n");
	}else{
		int now = 0;
		double LONGEST = capacity * d_avg, tank_now = 0, sum_price = 0;
		while(now < n){
			bool flag = false; 
			int idex = now + 1;
			int next = idex;
			double min_price = sta[idex].price;
			while(idex <= n && sta[idex].distance - sta[now].distance <= LONGEST){
				if(sta[idex].price < sta[now].price){
					next = idex;
					flag = true;
					break;
				}
				if(sta[idex].price < min_price){
					min_price = sta[idex].price;
					next = idex;
				}
				idex++;
			}
			if(sta[now].distance + LONGEST < sta[next].distance){
				printf("The maximum travel distance = %.2f", 1.0 * sta[now].distance + LONGEST);
				return 0;
			}
			double tmp_distance = sta[next].distance - sta[now].distance;
			if(flag){
				if(tank_now <= tmp_distance){
					sum_price += (tmp_distance - tank_now) * sta[now].price / d_avg;
					tank_now = 0;
				}else{
					tank_now -= tmp_distance;
				}   
			}else{
				sum_price += (LONGEST - tank_now) * sta[now].price / d_avg;
				tank_now = LONGEST - tmp_distance;
			}  
			now = next;
		}
		printf("%.2f", sum_price);
	}
	return 0;
}
发布了271 篇原创文章 · 获赞 5 · 访问量 6538

猜你喜欢

转载自blog.csdn.net/qq_42347617/article/details/104053178