PAT 1033. To Fill or Not to Fill (25) 贪心

来源:互联网 发布:淘宝无门槛红包设置 编辑:程序博客网 时间:2024/05/29 16:17

1033. To Fill or Not to Fill (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
ZHANG, Guochuan

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. 首先对输入数据按距离升序排序,从A站点开始,记加满油后最大行驶距离为max, 从A站点到A+max距离内搜索: 
  3. 1. 若搜索到第一个油价小于A的站点,则在A加油量为刚好保证能从A走到该站点。 
  4. 2. 若没有1里面的站点但是有终点,则A加油量为刚好保证能从A走到该终点。 
  5. 3. 若不是1,2情况但是此范围内有别的加油站,则在A加满油走到那些站点中油价最低的一个。 
  6. 4. 不是1,2,3情况,肯定是在此范围为没有任何站点,则最大行驶距离为A+max. 
  7. 注意:有个case是陷阱,没有距离为0的站点,则汽车无法行驶。油量不一定是整数!注意记录剩余油量! 
  8. 过程中到达某个油站时油箱可能会有多余的由(到下个油站一定用完),而终点时油箱一定没油
#include <iostream>#include<map>#include<iomanip>#include<string>#include<algorithm>using namespace std;int c,d,dd,n;struct ZZ{   float price;   float dist;   bool operator < (const ZZ &A) const    {        return dist<A.dist;    }}tutu[505]; int main() {        cin>>c>>d>>dd>>n;        for(int i=0;i<n;i++)        {            cin>>tutu[i].price;            cin>>tutu[i].dist;        }         tutu[n].dist=d;         tutu[n].price=99999;      int p1=0,p2=0;      float money=0;      int p3;      float gas=0;      sort(tutu,tutu+n);        int maxlen=c*dd;        while(1)         {  if(tutu[0].dist!=0) {cout<<"The maximum travel distance = 0.00";return 0;}             p2=p1;            while(tutu[p2].dist<=tutu[p1].dist+maxlen&&p2<=n)               p2++;           if(p2==p1+1)           {               cout<<"The maximum travel distance = ";               cout<<fixed<<setprecision(2)<<tutu[p1].dist+maxlen;               return 0;           }              float minprice=9999;              for(int i=p1+1;i<p2;i++)                {                    if(tutu[i].price<minprice)                        {                            p3=i;                            minprice=tutu[i].price;                            if(minprice<tutu[p1].price) break;                        }                }               if(tutu[p3].price<tutu[p1].price)               {                   float tm=(tutu[p3].dist-tutu[p1].dist)/dd;                     if(gas<(tutu[p3].dist-tutu[p1].dist)/dd  )                       money+=(tm-gas)*tutu[p1].price;                       gas=0;                       p1=p3;               }                  else {                            if(tutu[n].dist<=tutu[p1].dist+maxlen)                                {                                    money+=((tutu[n].dist-tutu[p1].dist)/dd-gas)*tutu[p1].price;                                    cout<<fixed<<setprecision(2)<<money;                                    return 0;                                }                          money+=(c-gas)*tutu[p1].price;                          gas=c-(tutu[p3].dist-tutu[p1].dist)/dd;                          p1=p3;                       }        } system("pause");    return 0;  }