Header Ads Widget

[Solved] Tram ride

 Problem(Tram ride)

A city has N Tram stations numbered from 1 to N  that are connected to one another and form a circle. You are given an array ticket_cost  where ticket_cost[i] is the cost of a ticket between the stops number i and (i + 1) % N. The Tram can travel in both directions i.e. clockwise and anti-clockwise.

Return the minimum cost to travel between the given start and finish station.

You are given an integer where N represents the total number of the tram stations, an integer start which represents the start station, and an integer finish which represents the finish station. You are given an array of positive integers  ticket_cost where ticket_cost[i] represents the ticket cost between the station number i and (i + 1) % N.

Task

Determine the minimum cost to travel between the given start and finish station.

Example

Assumptions

  • N = 4
  • start = 1
  • finish = 4
  • ticket_cost = [1, 2, 2, 4 ]

Approach

path1 -> 1------1-----> 2 -------2------> -------2------> 4 . => 1+2+2 => 5

path2 -> 1------4------>=> 4

Path2 will give the minimum cost. Therefore return 4.

Function description

Complete the Solve() function provided in the editor below that takes the following arguments and finds the minimum cost to travel between the given start and finish station:

  • N: Represents the total number of tram stations
  • start: Represents the start station
  • finish: Represents the finish station
  • ticket_cost: Represents ticket_cost[i] denoting the ticket-cost between the station number i and (i + 1) %N

Input format

  • The first line contains an integer denoting the total number of tram stations.
  • The second line contains an integer start denoting the start station.
  • The third line contains an integer finish denoting the finish station.
  • The fourth line contains an space-separated integer array ticket_costticket_cost[i] represents the ticket-cost between the station number i and (i + 1) %N.

Output format

Return the minimum cost to travel between the given start and finish station.

Constraints

1105

0,

0_[]107

Sample Input
4
1
3
1 2 3 4
Sample Output
3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

path1 -> 1------1-----> 2 -------2------> . => 1+2 => 3

path2 -> 1------4------>-------3------> . =>  4+3 => 7

Path1 will give the minimum cost. Therefore return 3.

Solution(C++):-


#include<bits/stdc++.h>
using namespace std;
long long solve (int N, int start, int finish, vector<int> Ticket_cost) {
long sumfor=0;
long long sum=0;
long suminv=0;
if(finish>start){
for(int i=start-1;i<finish-1;i++){
sumfor=sumfor+Ticket_cost[i];
}
for(int i=0;i<N;i++){
sum=sum+Ticket_cost[i];
}
suminv=sum-sumfor;
if(sumfor>suminv){
return suminv;
}
else
return sumfor;
}
////////////////////
if(finish<start){
for(int i=finish-1;i<start-1;i++){
sumfor=sumfor+Ticket_cost[i];
}
for(int i=0;i<N;i++){
sum=sum+Ticket_cost[i];
}
suminv=sum-sumfor;
if(sumfor>suminv){
return suminv;
}
else
return sumfor;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
int start;
cin >> start;
int finish;
cin >> finish;
vector<int> Ticket_cost(N);
for(int i_Ticket_cost = 0; i_Ticket_cost < N; i_Ticket_cost++)
{
cin >> Ticket_cost[i_Ticket_cost];
}
long long out_;
out_ = solve(N, start, finish, Ticket_cost);
cout << out_;
}




 Similar posts:-

  1. C-programming MCQs part-1
  2. C-programming MCQs part-2
  3. C-programming MCQs part-3
  4. C-programming MCQs part-4
  5. C-programming MCQs part-5
  6. C-programming MCQs part-6
  7. COA MCQs part-1
  8. COA MCQs part-2
  9. COA MCQs part-3
  10. COA MCQs part-4
  11. Microprocessor 8085 MCQs part-1
  12. Microprocessor 8085 MCQs part-2
  13. Microprocessor 8085 MCQs part-3
  14. CSS MCQs part-1
  15. CSS MCQs part-2
  16. CSS MCQs part-3
  17. CSS MCQs part-4
  18. CSS MCQs part-5
  19. Engineering Mathematics -II MCQs part-1
  20. Engineering Mathematics -II MCQs part-2
  21. Engineering Mathematics -II MCQs part-3
  22. Engineering Mathematics -II MCQs part-4
  23. Engineering Mathematics -II MCQs part-5
  24. Operating system MCQs part-1
  25. Operating system MCQs part-2
  26. Operating system MCQs part-3
  27. Operating system MCQs part-4
  28. Operating system MCQs part-5