all test cases are wrong but when i running the code in ide and on other compiler it is giving right output
Deepak and his journey
if(cost.size()==0) cost.push_back(a); else if(a>cost[cost.size()-1]) cost.push_back(cost[cost.size()-1]); else cost.push_back(a); can you explain these line no 17 to 21 please and also please explain what is wrong with my approach
mam please reply to the problem
@Learning_bunny
you can understan it through this implementation
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] . this in another simplified approach
#include<bits/stdc++.h>
using namespace std;
int main()
{
int tc;
cin>>tc; // Take input for test cases
while(tc–)
{
int n;
cin>>n; // Take input of n i.e. total check points
long long c[n+1],l[n+1]; // For simplicity prepare arrays of size n+1 to convert it to 1 based indexing
for(int i=1;i<=n;i++)
cin>>c[i]; //Take input for array C
for(int i=1;i<=n;i++)
cin>>l[i]; //Take input for array L
long long mi = 1000000LL; // Variable to take care of the minimum cost of petrol upto a checkpoint X
long long ans = 0;
for(int i=1;i<=n;i++)
{
mi = min(mi,c[i]); // Compare the cose of petrol between cost at current checkpoint and the minimum cost till now
ans+=mi*l[i];
}
cout<<ans<<endl;
}
return 0;
}
can you please tell me whats wrong with my code, or my approach is wrong but sanket bhaiya told the same approach in the hint video which i had used