Suppose there is a circle. There are petrol pumps on that circle. Petrol pumps are numbered to
(both inclusive). You have two pieces of information corresponding to
each of the petrol pump: (1) the amount of petrol that particular petrol
pump will give, and (2) the distance from that petrol pump to the next
petrol pump.
Initially, you have a tank of infinite capacity carrying no petrol. You can start the tour at any of the petrol pumps. Calculate the first point from where the truck will be able to complete the circle. Consider that the truck will stop at each of the petrol pumps. The truck will move one kilometer for each litre of the petrol.
Initially, you have a tank of infinite capacity carrying no petrol. You can start the tour at any of the petrol pumps. Calculate the first point from where the truck will be able to complete the circle. Consider that the truck will stop at each of the petrol pumps. The truck will move one kilometer for each litre of the petrol.
Input Format
The first line will contain the value of .
The next lines will contain a pair of integers each, i.e. the amount of petrol that petrol pump will give and the distance between that petrol pump and the next petrol pump.
Constraints:
The next lines will contain a pair of integers each, i.e. the amount of petrol that petrol pump will give and the distance between that petrol pump and the next petrol pump.
Constraints:
Output Format
An integer which will be the smallest index of the petrol pump from which we can start the tour.
Sample Input
3
1 5
10 3
3 4
Sample Output
1
Explanation
We can start the tour from the second petrol pump.
#include <string.h>
#include <math.h>
#include <stdlib.h>
typedef int bool;
#define true 1
#define false 0
typedef struct station {
int petrol;
int dist;
} station_t;
bool
check_path(station_t *s,int start,int n) {
int fuel = 0;
int i = start;
while (i < n) {
fuel += s[i].petrol;
fuel -= s[i].dist;
if (fuel < 0) {
return false;
}
i++;
if (start > 0) {
if (i == start) {
return true;
} else if (i == n) {
i = 0;
}
}
}
return true;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,i,rot=-1;
station_t *s;
scanf("%d",&n);
s = malloc(n * sizeof(station_t));
if (NULL == s) {
fprintf(stderr,"malloc failed\n");
return(-1);
}
for (i=0;i<n;i++) {
scanf("%d",&s[i].petrol);
scanf("%d",&s[i].dist);
}
while (++rot < n) {
if (check_path(s,rot,n)) {
break;
}
}
printf("%d\n",rot);
free(s);
return 0;
}
Truck Tour- Hacker Rank Solution
#include <stdio.h>#include <string.h>
#include <math.h>
#include <stdlib.h>
typedef int bool;
#define true 1
#define false 0
typedef struct station {
int petrol;
int dist;
} station_t;
bool
check_path(station_t *s,int start,int n) {
int fuel = 0;
int i = start;
while (i < n) {
fuel += s[i].petrol;
fuel -= s[i].dist;
if (fuel < 0) {
return false;
}
i++;
if (start > 0) {
if (i == start) {
return true;
} else if (i == n) {
i = 0;
}
}
}
return true;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,i,rot=-1;
station_t *s;
scanf("%d",&n);
s = malloc(n * sizeof(station_t));
if (NULL == s) {
fprintf(stderr,"malloc failed\n");
return(-1);
}
for (i=0;i<n;i++) {
scanf("%d",&s[i].petrol);
scanf("%d",&s[i].dist);
}
while (++rot < n) {
if (check_path(s,rot,n)) {
break;
}
}
printf("%d\n",rot);
free(s);
return 0;
}
No comments:
Post a Comment