一道贪心题目(To Fill or Not to Fill )

来源:互联网 发布:kali 安装yum 编辑:程序博客网 时间:2024/06/08 06:15


遇到一道挺不错的贪心题目,记录一下:


/*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、如果当前油量不足以行驶到这个油价更便宜的加油站,则加油到刚好能够到达此加油站的油量,行驶过去,此时,油量剩余为0。

       二、当从当前加油站往后可达的加油站序列中寻找不到油价比当前加油站价格更低的加油站,则寻找到在可达范围内油价最便宜的加油站(此时这个加油站的油价>当前加油站的油价),这种情况,在当前加油站加满油,行驶过去,计算剩余油量。

       三、在新的起点,新的加油站,重复以上策略。

       四、考虑,如果第一个加油站如果不在起点,则最大行驶距离为0,特殊处理。

代码如下:

 

#include <cstdio>#include <iostream>#include <algorithm>#include <string.h>using namespace std;typedef struct station{double price;double dist;bool operator<(const station& a)const{return dist<a.dist;}}station;station curr[505];int main(){double Cmax,Dist,Davg;int i,j,N,next;double total_cost,min_price,remain_gas,full_dist;freopen("f:/in.txt","r",stdin);while(~scanf("%lf%lf%lf%d",&Cmax,&Dist,&Davg,&N)){for(i=0;i<N;i++){scanf("%lf%lf",&curr[i].price,&curr[i].dist);}sort(curr,curr+N);full_dist=Cmax*Davg;//加满油最多可以跑的距离curr[N].dist=Dist;curr[N].price=0;if(curr[0].dist!=0)   //  起始点没有加油站,特殊处理{printf("The maximum travel distance = 0.00\n");continue;}total_cost=0;remain_gas=0;for(i=0;i<N;){min_price=curr[i+1].price;next=i+1;for(j=i+1;j<=N;j++)   //寻找下一个加油站{if(curr[j].dist<=curr[i].dist+full_dist&&curr[j].price<=curr[i].price){next=j;break;}if(curr[j].dist<=curr[i].dist+full_dist&&min_price>curr[i].price){min_price=curr[j].price;next=j;}elsebreak;    //无法到达此加油站以及更远的加油站}if(curr[next].dist<=curr[i].dist+full_dist)  //加满油,可以到达下一站?{if(curr[next].price<=curr[i].price){if(remain_gas>=(curr[next].dist-curr[i].dist)/Davg)//剩余油足够跑到下一个价格更低的加油站{remain_gas-=(curr[next].dist-curr[i].dist)/Davg; //跑到下一个加油站next后剩余的油数}else                                               //加够油量,刚好跑到下一个价格更低的加油站{      total_cost+=((curr[next].dist-curr[i].dist)/Davg-remain_gas)*curr[i].price;remain_gas=0; //跑到下一个加油站next后剩余的油数}}else{total_cost+=(Cmax-remain_gas)*curr[i].price*1.0;//如果下一个加油站的油价比当前加油站油价贵,在当前加油站加满油remain_gas=Cmax-(curr[next].dist-curr[i].dist)/Davg; //跑到下一个加油站next后剩余的油数}}elsebreak;i=next;    //更新此时的位置,下一个加油站,}if(i==N)printf("%.2lf\n",total_cost);elseprintf("The maximum travel distance = %.2lf\n",curr[i].dist+full_dist);}fclose(stdin);return 0;}


原创粉丝点击