zoukankan      html  css  js  c++  java
  • PAT 1033 To Fill or Not to Fill

    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​​ (≤), the distance between this station and Hangzhou, for ,. 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 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

     1 #include<iostream>
     2 #include<vector>
     3 #include<algorithm>
     4 const double inf = 99999999.0;
     5 struct Node{
     6   double pri;
     7   int dis;
     8 };
     9 bool cmp(Node& a, Node& b){return a.dis<b.dis;}
    10 using namespace std;
    11 /*
    12   *******注意输入double类型要用%lf
    13   当确信自己的算法没有问题的时候, 还是有测试点出错 那可能是自己漏掉了信息 或者没考虑一些边界条件
    14 */
    15 int main(){
    16   double cMax, dAvg;
    17   int n, i, j, d;
    18   scanf("%lf%d%lf%d", &cMax, &d, &dAvg, &n);
    19   vector<Node> v(n);
    20   for(i=0; i<n; i++)scanf("%lf%d", &v[i].pri, &v[i].dis);
    21   Node node;
    22   node.dis=d; node.pri=inf;
    23   v.push_back(node);
    24   sort(v.begin(), v.end(), cmp);
    25   double sum=0, tank=0;
    26   bool flag = true;
    27   //坑! 出发点可能没有油站 不能出发
    28   if(v[0].dis!=0){ printf("The maximum travel distance = 0.00"); return 0;}
    29   for(i=0; i<n; ){
    30      if(v[i+1].dis-v[i].dis>cMax*dAvg){
    31          flag = false;
    32        printf("The maximum travel distance = %.2f", v[i].dis+cMax*dAvg);
    33        break;
    34      }else{
    35          if(v[i+1].pri<=v[i].pri){
    36          sum += ((v[i+1].dis-v[i].dis)/dAvg-tank)*v[i].pri;
    37              tank=0;
    38              i++;
    39        }else{
    40          int minidx=i+1; //找后面续航范围内最便宜的油站
    41          for(j=i+1; j<n&&((v[j].dis-v[i].dis)<=cMax*dAvg); j++)
    42            if(v[minidx].pri>=v[j].pri) minidx = j;
    43          if(v[i].pri<=v[minidx].pri){
    44               if((d-v[i].dis)<=cMax*dAvg){//该站到终点距离小于最大续航能力
    45                     sum += ((d - v[i].dis)/dAvg-tank)*v[i].pri;
    46                     break;
    47              }else{//该站到终点距离大于续航能力 把油加满, 到续航范围内最便宜的加油站加油
    48                   sum += (cMax - tank)*v[i].pri;
    49                   tank = cMax - (v[minidx].dis - v[i].dis)/dAvg;
    50              }
    51          }else{
    52            sum += ((v[minidx].dis - v[i].dis)/dAvg-tank)*v[i].pri;
    53                tank = 0;
    54          }
    55             i = minidx;
    56        }
    57      }
    58   }
    59   if(flag) printf("%.2f", sum);
    60   return 0;
    61 }
  • 相关阅读:
    ubuntu sudo 号令有效处理
    shell脚本的主笔、存储、实行
    Oracle 11gR1中细粒度访谒收集处事(4)
    Solaris效力经管工具 SMF快速入门指南(1)
    Solaris 10完成宁静Kerberos身份验证(2)
    高质量C /C编程指南第2章 程序的版式
    Solaris 10下设置EJB 3.0景遇(1)
    高质量C /C编程指南序言
    Java学习之路:不走弯路,就是捷径 www.51testing.com
    两个结构体可以直接赋值吗? 回复 "JohnsonAnother" 的问题
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9580640.html
Copyright © 2011-2022 走看看