Click here to Skip to main content
15,889,992 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: Time complexity Pin
Richard MacCutchan28-Jun-18 0:58
mveRichard MacCutchan28-Jun-18 0:58 
GeneralRe: Time complexity Pin
Richard Deeming28-Jun-18 1:08
mveRichard Deeming28-Jun-18 1:08 
GeneralRe: Time complexity Pin
Richard MacCutchan28-Jun-18 1:20
mveRichard MacCutchan28-Jun-18 1:20 
AnswerRe: Time complexity Pin
Gerry Schmitz27-Jun-18 9:55
mveGerry Schmitz27-Jun-18 9:55 
GeneralRe: Time complexity Pin
Manish16927-Jun-18 17:25
Manish16927-Jun-18 17:25 
GeneralRe: Time complexity Pin
Gerry Schmitz27-Jun-18 19:16
mveGerry Schmitz27-Jun-18 19:16 
GeneralRe: Time complexity Pin
Peter_in_278027-Jun-18 18:19
professionalPeter_in_278027-Jun-18 18:19 
QuestionWhich algorithm or a solution should I use here? Pin
Member 1387019212-Jun-18 13:06
Member 1387019212-Jun-18 13:06 
Hello everyone,

I have been struggling since previous month about some question that I found on some university,
and I just cant think about a good way to solve it.
Please, help me to think about a way to solve this question.

The most important thing is that the solution has to work as fast as it can with the lowest complexity
that possible.

The question :

Quote:
Vik the puffin is planning a long road trip around the circle road in Iceland, during which he wants to visit
all the landmarks along a path of length L. The tank of Vik's car can take up to F units of fuel and for every
unit of distance covered, his car consumes a unit of fuel. Using Google maps, Vik knows how far each of
the N gas stations are from the beginning of the path and the price per fuel unit each station offers. At the
starting point he has T units of fuel in his car.
1. Task
Write a program that will accept the above information and will calculate the minimum amount of money
Vik needs to spend on gas. If the journey is impossible to make, it should print -1.
2. Input
The first line contains four space separated integers:
N (0 < N < 50001): The total number of gas stations
F (0 < F < 1000001): The units of fuel Vik's car can take
T (0 <= T <= F): The units of fuel Vik's car has at the beginning of the trip
L (0 < L < 1000000001): The path length of the landmarks he plans to visit
Each of the following N lines will contain two integers: the first one, D_i (0 <= D_i <= L) corresponds
to the distance of the station from the starting point, and the second one, C_i (1 <= C_i <= 1,000,000)
represents the cost per fuel unit for that station.
Note: You may assume that the trip will be on a straight line where all gas stations are
spread on this line at the positions specified by their D_i values.
3. Output
The minimum amount of money to be spent or with -1 in case the trip is not feasible.
Note: There is a newline character at the end of the last line of the output.
4. Sample input
4 20 6 34
4 40
18 15
10 7
20 12
2
5. Sample output
348
6. Explanation
The rst line of the input is 4 20 6 34 which means that:
a. There are in total N=4 gas stations on the route
b. The (max) fuel capacity of Vik's car is F=20 liters
c. The tank currently has T=6 liters of gas
d. Vik wants to travel L=34 kms in total
Then the details for the 4 gas stations are provided in the form Di Ci, where Di is the distance of this
gas station from the starting point and Ci is the cost per liter of gas:
4 40
18 15
10 7
20 12
For simplicity assume that the whole trip is done in a straight line as depicted below:

link to show the trip from the example -> Vik The Puffin — imgbb.com[^]


Obviously Vik does not have enough fuel for all 34 kms, so he needs to refuel. The cheapest gas station is
the one labeled (B) above, however Vik does not (initially) have enough fuel in his tank to reach (B), since
B-S = 10 and he has T=6. So he needs to add an extra 4 liters from gas station A, so that he can the make
it until gas station B to get as much (cheap) as he can in order to make his 34 km journey. Thus he pays
(i) 4lt * 40$/lt = 160¿ and now he can make it until (B). Since until this moment he has only traveled
10 kms, he needs gas for another 34-10=24kms. Normally he would want to refuel his car with 24 liters
(since B is the cheapest gas station) but since his (max) fuel capacity is F=20 liters he will only take 20
liters and thus pay (ii) 20 lt * 7 $/lt = 140$. He knows however that up to point (B) he has only traveled
10kms and he needs to travel another 24kms to reach his goal, whereas he has gas for 20kms. So he would
have to stop at a later gas station (after he has traveled at least 4kms) to refuel another 4 liters of gas so
that he could complete the whole 34 kms journey. Since he now has quite some gas, he may decide whether
he wants to refuel at (C) or at (D) and since (D) is cheaper, it is more than 4kms away from (B) and is
within reach (based on his gas in the tank) he will choose to refuel another 4 liters at (D) and thus pay (iii)
4lt*12$/lt=48$). After that he can successfully reach the end point of his trip.



PLEASE HELP ME WITH THIS!

Thanks in advance!
AnswerRe: Which algorithm or a solution should I use here? Pin
Gerry Schmitz14-Jun-18 7:36
mveGerry Schmitz14-Jun-18 7:36 
AnswerRe: Which algorithm or a solution should I use here? Pin
Eddy Vluggen14-Jun-18 8:02
professionalEddy Vluggen14-Jun-18 8:02 
AnswerRe: Which algorithm or a solution should I use here? Pin
Patrice T1-Sep-18 15:56
mvePatrice T1-Sep-18 15:56 
Questionlooking for tutorial heuristic algorithms Pin
Member 1386820111-Jun-18 7:57
Member 1386820111-Jun-18 7:57 
AnswerRe: looking for tutorial heuristic algorithms Pin
Gerry Schmitz14-Jun-18 7:32
mveGerry Schmitz14-Jun-18 7:32 
SuggestionAn algorithm checking balance of html tags Pin
Member 138554082-Jun-18 0:51
Member 138554082-Jun-18 0:51 
GeneralRe: An algorithm checking balance of html tags Pin
Jochen Arndt4-Jun-18 3:08
professionalJochen Arndt4-Jun-18 3:08 
GeneralRe: An algorithm checking balance of html tags Pin
Gerry Schmitz5-Jun-18 8:02
mveGerry Schmitz5-Jun-18 8:02 
GeneralRe: An algorithm checking balance of html tags Pin
Richard Deeming6-Jun-18 1:15
mveRichard Deeming6-Jun-18 1:15 
GeneralRe: An algorithm checking balance of html tags Pin
Eddy Vluggen6-Jun-18 1:24
professionalEddy Vluggen6-Jun-18 1:24 
GeneralRe: An algorithm checking balance of html tags Pin
Richard Deeming6-Jun-18 1:26
mveRichard Deeming6-Jun-18 1:26 
GeneralRe: An algorithm checking balance of html tags Pin
Eddy Vluggen6-Jun-18 1:39
professionalEddy Vluggen6-Jun-18 1:39 
GeneralRe: An algorithm checking balance of html tags Pin
Gerry Schmitz6-Jun-18 5:42
mveGerry Schmitz6-Jun-18 5:42 
GeneralRe: An algorithm checking balance of html tags Pin
Richard Deeming6-Jun-18 5:53
mveRichard Deeming6-Jun-18 5:53 
GeneralRe: An algorithm checking balance of html tags Pin
Gerry Schmitz6-Jun-18 6:25
mveGerry Schmitz6-Jun-18 6:25 
QuestionRecursive Best First Search for Pathfinding? Pin
Robert Vandenberg Huang26-May-18 3:29
professionalRobert Vandenberg Huang26-May-18 3:29 
AnswerRe: Recursive Best First Search for Pathfinding? Pin
Gerry Schmitz29-May-18 7:20
mveGerry Schmitz29-May-18 7:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.