Blue Bridge Cup algorithm training ALGO-15 Traveler's Budget

Source: Internet
Author: User

Algorithm training Traveler's Budget time limit: 1.0s memory Limit: 256.0MB problem description A traveler wants to drive a car with the least amount of money from one city to another (assuming the fuel tank is empty at the time of departure). Given the distance between the two cities D1, the capacity of the car tank C (in liters), the distance per liter of petrol D2, the starting point per liter of petrol price p and the number of oil stations along the way N (n can be 0), oil station I away from the starting point di, per liter gasoline price pi (i=1,2, ...) N). The calculation results are rounded to two digits after the decimal point. If the destination cannot be reached, the output is "No solution". Input format first behavior 4 real numbers D1, C, D2, p and a nonnegative integer n;
Next n rows, two real di, Pi per line. Output format if you can reach the destination, output a real number (rounded to two digits after the decimal point), indicating the minimum cost; otherwise output "no solution" (without quotation marks). Sample input 275.6 11.9 27.4 2.8 2
102.0 2.9
220.0 2.2 Sample Output 26.95 Topic Analysis:

Think of the starting point as the No. 0 gas station, the end of the N+1 gas station.
The distance from the starting point of the No. 0 gas station is 0, the distance of the first N+1 gas station from the beginning is D1
The price of the No. 0 petrol station is P, and the price of N+1 gas station is 0
According to the fuel tank capacity C and the distance per liter of petrol can travel D2 can calculate the maximum distance of oil tank fill can travel Maxdis

No solution: (at the input of the judgment, as soon as possible to find out the situation without solution)
If the distance of two petrol stations is greater than the maximum distance that the oil can travel, then no solution


There are solutions:
Find the nearest and cheapest petrol station from the next gas station at your current location:
1. If you can find the
(1) If you can arrive at once, then add to just to get to the cheap gas station can
(2) If you can not arrive at one time, then fill the tank first, to reach the maximum distance before the gas station, and then to the right to the cheap gas station
2. If not found, then fill the oil, travel to the maximum distance before the gas station, continue to look for
Note: At the end of the gas station price is 0, so the last refueling to just reach the end of the

Example code:
1#include <iostream>2#include <cstdio>3 using namespacestd;4 5 #defineMax_num 10016 7 intMain ()8 {9     intN;Ten     DoubleD1, C, D2, P; Onescanf"%lf%lf%lf%lf%d", &d1, &c, &d2, &p, &N); A  - //double * distance = new Double[n+5]; //distance of the gas station I to the beginning - //double * Price = new DOUBLE[N+5]; //the price of oil the     DoubleDistance[max_num]; -     DoublePrice[max_num]; -  -distance[0] =0;//record the distance from the starting point of the first I +price[0] = P;//beginning of the oil price -distance[n+1] = D1;//end of the last gas station . +price[n+1] =0;//End Price A  at     DoubleTotal =0;//Fees -     Doublesurplus=0;//The remaining amount of oil at the first petrol station -     DoubleMaxdis = C * D2;//maximum distance exercised by full oil -     BOOLFlag =true;//If there is a solution, the default has a solution -      -      for(inti =1; I <= N; i++) in     { -scanf"%LF%LF", &distance[i], &price[i]); to         if(Distance[i]-distance[i-1] > Maxdis)//if the distance of two petrol stations is greater than the distance that can be exercised by the filling oil, then no solution +         { -Flag =false;//No solution the         }  *     } $     Panax Notoginseng     if(!flag)//No solution -     { theprintf"No solution\n"); +         return 0;  A     } the  +     /* - I: Number of the current gas station $ J: The next number of gas stations cheaper than yourself $     */  -      for(inti =0, J; I <= N; i = j)//when J is reached, I will be assigned a value of J to re-cycle and know where to reach the end -     { the          for(j = i +1; J <= N +1; J + +)//from the next start of I find a gas station cheaper than it -         {    Wuyi             if(Distance[j]-distance[i] > Maxdis)//If you can't exercise to a gas station that's cheaper than it the             { -j--;//The gas station before it reaches the maximum distance after filling the oil. Wu                  Break; -             } About             if(Price[j] <= price[i])//find a gas station that's cheaper than the current gas station . $             { -                  Break; -             }  -         } A  +         /* the find the nearest and cheapest petrol station from the next gas station at your current location: - 1 If we can find it. $ (1) If you can arrive at once, then add to just to get to the cheap gas station can the (2) If you can not arrive at one time, then fill the tank first, to reach the maximum distance before the gas station, and then to the right to the cheap gas station the 2 If not found, then fill the oil, travel to the maximum distance before the gas station, continue to look for the         */ the          -         if(Price[j] <= price[i])//belongs to (1) in         { theTotal + = ((Distance[j]-distance[i])/d2-surplus) * Price[i];//added to the point where you can just exercise to J. theSurplus =0;//remaining oil quantity About         } the         Else    //belongs to (2) or 2 the         { theTotal + = (c-surplus) * Price[i];//Tank Filling +Surplus = C-(distance[j]-distance[i])/D2;//The amount of oil remaining at J Point -         } the     }Bayi      theprintf"%.2lf\n", total); the          -     return 0; -}

Blue Bridge Cup algorithm training ALGO-15 Traveler's Budget

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.