Ques: https://hack.codingblocks.com/contests/c/547/1012
code: https://ide.codingblocks.com/s/56031
Its a really tedious code but I have tried my best to explain it
and also written my greedy aproach that i am using in it.
Ques: https://hack.codingblocks.com/contests/c/547/1012
code: https://ide.codingblocks.com/s/56031
Its a really tedious code but I have tried my best to explain it
and also written my greedy aproach that i am using in it.
can not understand your logic it is very very tedious, you just need to minimum cost of petrol till the current check post. from minimum you will use and then calculate cost of travel using this.
logic is wrong.
You just have to find minimum till current index, you will use that petrol, and to find cost multiply it with the distance and find total cost.
This is a simple greedy problem. We just need to buy petrol from the minimum possible rates checkpoints. Lets say we are at checkpoint X. Now there are X+1 checkpoints up to this checkpoint. We can easily see that for buying petrol for going to checkpoint X+1 , we just need to find the minimum possible C[i] where i can be from 1 to X+1 . Thus, we can just compare the minimum petrol rate till now(lets say denote it by (mi) at each of the checkpoint with the checkpoint rate i.e. C[i] . Now two possible cases arise:
First Case: mi <= C[i], No need to update mi i.e. petrol for this checkpoint has to be bought with the rate .
Second Case: mi > C[i] , We need to update mi and petrol for this checkpoint has to be bought with the new rate i.e. C[i] .
After iterating through all checkpoints following the above algorithm will lead to minimum travel cost.
Hit like if you get it.
tysm 
I was actually doing the same thing but due to the tediousness, my logic went wrong.
The smaller code was much better TY.