【PAT】1033. To Fill or Not to Fill (25)

来源:互联网 发布:新潮软件 编辑:程序博客网 时间:2024/05/28 15:35

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: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=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: Pi, the unit gas price, and Di (<=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 86.00 12507.00 6007.00 1507.10 07.20 2007.50 4007.30 10006.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 27.10 07.00 600
Sample Output 2:
The maximum travel distance = 1200.00


分析:

贪心问题。在便宜的站点多加油。

分以下几种情况考虑:

(1)目前剩余的油可以找到最近的比目前加油站便宜的站点。那么不用加油,直接开往较便宜的站点;

(2)若不满足(1),但是如果在目前站点加满油可以到达下一个比目前加油站便宜的站点,那么就加油使其刚好可以开到较便宜的站点;

(3)若不满足(1)与(2),也就是说所能到达的最远站点都比目前加油站要贵,那么就要在目前这个站点加满油,开往所能到达的最远的站点。


Tip: 可以将终点也考虑成为一个加油站。

参考  sunbaigui 的博客:http://blog.csdn.net/sunbaigui/article/details/8657146#reply


#include<iostream>#include<vector>#include<algorithm>using namespace std;double maxgas; //最大容量double Distance; //距离double ave; //每单位油可开距离struct Node{double price;double dis;Node(double p,double d):price(p),dis(d){}Node(){}};int FindCheaper(int i, vector<Node> v, double gas){int j;int index = i;double dis_temp = gas * ave;double p_temp = v[i].price;for(j = i+1; j<v.size() && (v[j].dis-v[i].dis)<=dis_temp; j++){if(v[j].price < p_temp){index = j;break;}}return index;}int FindFurthest(int i,vector<Node> v,double gas){int j;int index = i;double dis_temp = gas  * ave;for(j = i+1; j<v.size() && (v[j].dis-v[i].dis) <= dis_temp; j++ ){index = j;}return index;}bool cmp(Node a,Node b){return a.dis < b.dis;}int main(){int N;scanf("%lf %lf %lf %d",&maxgas,&Distance,&ave,&N);int i;double p;double dis;vector<Node> vec;for(i=0; i<N; i++){scanf("%lf %lf",&p,&dis);Node n(p,dis);vec.push_back(n);}Node final;final.dis = Distance;vec.push_back(final);sort(vec.begin(),vec.end(),cmp);if(vec[0].dis != 0 || N==0){printf("The maximum travel distance = 0.00\n");return 0;}double remain_gas = 0.0;int index;double money = 0.0;for(i=0; i<N; ){//用剩下的油寻找最近最便宜的加油站index = FindCheaper(i,vec,remain_gas);if(index != i){ //找到了remain_gas -= (vec[index].dis - vec[i].dis)/ave;i = index; //到达这个更便宜的加油站continue;}//用最大的油寻找最近最便宜的加油站index = FindCheaper(i,vec,maxgas);if(index != i){money += ( (vec[index].dis-vec[i].dis)/ave - remain_gas ) * vec[i].price;remain_gas = 0.0; //只要加油刚好到达这个便宜的站点就好,所以剩余油量为0i = index;continue;}//找不到更便宜的,所以就在目前站点把油加满index = FindFurthest(i,vec,maxgas);if(index != i){ money += (maxgas - remain_gas) * vec[i].price;remain_gas = maxgas -  (vec[index].dis-vec[i].dis)/ave;i = index;continue;}else {printf("The maximum travel distance = %.2lf\n",vec[i].dis + maxgas*ave);return 0;}}if(i == N){printf("%.2lf\n",money);}return 0;}


原创粉丝点击