title: 2019-3 To Fill or Not to Fill (30 分) I cannot AC for I can't get through "last dist is exactly D"
body: <p>the problem is:
  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.</p>
  
  <p>Input Specification:
  Each input file contains one test case. For each case, the first line contains 4 positive numbers: C
  ​max
  ​​  (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D
  ​avg
  ​​  (≤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: P
  ​i
  ​​ , the unit gas price, and D
  ​i
  ​​  (≤D), the distance between this station and Hangzhou, for i=1,⋯,N. All the numbers in a line are separated by a space.</p>
  
  <p>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.</p>
  
  <p>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</p>
  
  <p>I use the greedy algorithm to solve this problem, first fill the tank to reach the nearest and cheapest station, if we don't have a station like that, then we may reach the destination or find a cheaper station in the range which we can reach.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #define N 500
  typedef struct Station
  {
      int distance;
      double price;
  }Station_t;
  
  int compare(const void *p1, const void *p2)
  {
      const Station_t *pleft = (Station_t *)p1;
      const Station_t *pright = (Station_t *)p2;
      return pleft-&gt;distance &lt; pright-&gt;distance ? -1 : pleft-&gt;distance &gt; pright-&gt;distance;
  }
  
  int main()
  {
      int capacity, totalDistance, davg, num, i;
      double usedMoney = 0;
      double usingC = 0;
      scanf("%d %d %d %d", &amp;capacity, &amp;totalDistance, &amp;davg, &amp;num);
      Station_t stas[N];
      for(i = 0; i &lt; num; ++i)
      {
          scanf("%lf %d", &amp;stas[i].price, &amp;stas[i].distance);
      }
      stas[num].distance = totalDistance;
      stas[num].price = 0;
      qsort(stas, num, sizeof(Station_t), compare);
      if(stas[0].distance &gt; 0)
      {
          printf("The maximum travel distance = 0.00");
          return 0;
      }
      Station_t *cur = stas, *next = &amp;stas[1];
      while(cur != &amp;stas[num - 1])
      {
          while(next-&gt;distance - cur-&gt;distance &lt;= davg * capacity)
          {
              if(next-&gt;price &lt; cur-&gt;price)
              {
                  usedMoney += ((double)(next-&gt;distance - cur-&gt;distance - davg * usingC) / (double)davg) * cur-&gt;price;
                  cur = next;
                  if(cur == &amp;stas[num - 1])
                      break;
                  ++next;
              }
              else
              {
                  if(next == &amp;stas[num - 1])
                      break;
                  ++next;
              }
          }
          if(cur == &amp;stas[num - 1])
              break;
          usedMoney += (capacity - usingC) * cur-&gt;price;
          Station_t *temp = cur + 1, *fin = temp;
          while(fin != next)
          {
              if(fin-&gt;price &lt; temp-&gt;price)
                  temp = fin;
              ++fin;
          }
          usingC = capacity - (double)(temp-&gt;distance - cur-&gt;distance) / (double)davg;
          cur = temp;
      }
      if(totalDistance - cur-&gt;distance &lt;= davg * capacity)
      {
          usedMoney += (double)(totalDistance - cur-&gt;distance) / (double)davg * cur-&gt;price;
          printf("%.2lf", usedMoney);
      }
      else
      {
          printf("The maximum travel distance = %.2f", (float)(cur-&gt;distance + davg * capacity));
      }
      return 0;
  }
  </code></pre>
  
  <p>I cannot get through a sample named "last dist is exactly D"</p>