PAT 甲级 1033 To Fill or Not to Fill

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a845717607/article/details/86608126

1033 To Fill or Not to Fill (25 point(s))

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 = Xwhere 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

Experiential Summing-up

The significant point need to notice is Cmax,Darg,D and every station's distance are double float type. Or you will get wrong answer. As for the greedy algorithm, it did need profoundly understand. See the code for details. That's all~ 

(The purpose of using English to portray my solution is that to exercise the ability of my expression of English and accommodate PAT advanced level's style.We can make progress together by reading and comprehending it. Please forgive my basic grammar's and word's error. Of course, I would appreciated it if you can point out my grammar's and word's error in comment section.( •̀∀•́ ) Furthermore, Big Lao please don't laugh at me because I just a English beginner settle for CET6    _(:з」∠)_  )

Accepted Code

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=510;
const int INF=0x3fffffff;
double Cmax,D,Darg;
int n;
struct station
{
	double price;
	double dis;
}sta[maxn];
bool cmp(station a,station b)
{
	return a.dis<b.dis;
}
int main()
{
	scanf("%lf %lf %lf %d",&Cmax,&D,&Darg,&n);
	for(int i=0;i<n;++i)
		scanf("%lf %lf",&sta[i].price,&sta[i].dis);
	sta[n].dis=D;
	sta[n].price=0;
	sort(sta,sta+n+1,cmp);
	if(sta[0].dis!=0)
		printf("The maximum travel distance = 0.00\n");
	else
	{
		double cost=0,tank=0,curlen=0,maxdis=Cmax*Darg;
		curlen+=maxdis;
		int pos=0;
		for(int i=1;i<=n;++i)
		{
			if(sta[i].dis>D)
				break;
			if(sta[i].dis<=curlen&&sta[i].dis+maxdis>=curlen)
			{
				int first=0,min=i;
				for(int j=i;j<=n;++j)
				{
					if(sta[j].dis<=curlen&&sta[j].dis+maxdis>=curlen)
					{
						if(sta[j].price<sta[min].price)
						{
							min=j;
						}
						if(sta[j].price<sta[pos].price)
						{
							first=j;
							break;
						}
					}
					else
						break;
				}
				if(first!=0)
				{
					double need=(sta[first].dis-sta[pos].dis)/Darg;
					if(tank<need)
					{
						cost+=(need-tank)*sta[pos].price;
						tank=0;
					}
					else
					{
						tank-=need;
					}
					curlen=sta[first].dis+maxdis;
					i=pos=first;
				}
				else
				{
					cost+=(Cmax-tank)*sta[pos].price;
					tank=Cmax-(sta[min].dis-sta[pos].dis)/Darg;
					curlen=sta[min].dis+maxdis;
					i=pos=min;
				}
			}
			else
				break;
		}
		if(pos==n)
			printf("%.2f",cost);
		else
			printf("The maximum travel distance = %.2f\n",curlen);
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a845717607/article/details/86608126