To Fill or Not to Fill<2011浙大复试机试>

题目描述: 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.
输入: 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.
输出: 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.
样例输入: 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 50 1300 12 2 7.10 0 7.00 600
样例输出: 749.17 The maximum travel distance = 1200.00 提示: 此代码可在九度AC,但在pat有个样例一直无法通过,个人感觉可能与精度有关

最难理解的部分是next(),已注释

#include <stdio.h>
 #include <algorithm>
 #include <math.h>
 using namespace std;
 
 double sum, gl, maxd, maxdis, Cmax, D, per;
 bool available;
 int N;
 struct gas {
     double price, d;
     bool operator <(const gas &g) const {
         return d < g.d;
     }
 } buf[501];
 
 /*
 void next(站号){
     把站号设为下一站
     k = k + 1;
     if(不是第一站)
         计算剩余油量 = 该站与上一站距离差 / 每单位油行驶距离;
     for(从下一站开始查找更便宜的加油站,直到找到更便宜的或者到达最后一站即N + 1);
     if(找到的站距离当前站较远)
         加满油
     else{
         span = 从当前站到达找到的站的耗油量 - 还剩余的油量;
         if(span > 0)说明当前油量不足以到达查找到的站,需要加油
             加油;
     }
 }
 */
 void next(int i) {
     int k = i + 1;
     if (i != 1)
         gl -= (buf[i].d - buf[i - 1].d) / per;
     for (; k < N + 1 && buf[k].price >= buf[i].price; k++); 
     if ((int)(buf[k].d - buf[i].d) > (int)maxdis) {
         sum += (Cmax - gl) * buf[i].price;
         gl = Cmax;
     } else {
         double span = (buf[k].d - buf[i].d) / per - gl;
         if (fabs(span) > 1e-8 && span > 0) {
             sum += span * buf[i].price;
             gl = (buf[k].d - buf[i].d) / per; 
         }
     }
 }
 
 int main() {
     while (scanf("%lf%lf%lf%d", &Cmax, &D, &per, &N) != EOF) {
         maxd = sum = gl = 0;
         available = true;
         maxdis = Cmax * per;
         //加满一次油行驶最大距离
         for (int i = 1; i <= N; i++)
             scanf("%lf%lf", &buf[i].price, &buf[i].d);
         buf[N + 1].price = 1000000;    
         //初始化目的地价格
         buf[N + 1].d = D;
         sort(buf + 1, buf + N + 2);
 
         if (buf[1].d > 0) {
             printf("The maximum travel distance = 0.00\n");
             continue;
         }
 
         for (int i = 1; i <= N; i++) {
             next(i);
             //用于计算价格
             if (buf[i + 1].d - buf[i].d > maxdis) {
                 maxd = buf[i].d + maxdis;
                 available = false;
                 printf("The maximum travel distance = %.2lf\n", maxd);
                 break;
             }
         }
         if(available)
             printf("%.2lf\n", sum);
     }
     return 0;
 }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值