Menu Close

Find the status of the passengers and safari cars at zoo after k units of time.

A Zoo consists of a lion museum and a zoo for safari riding. There are many passengers and n single-passenger cars. Passengers wander around the museum for a while and then line up at the zoo gate to take a ride in a safari car. 

Note that each passenger is allowed for one ride only. 

When a car is available, it loads the one passenger it can hold and rides the zoo for a specific amount of time say p

If all the n cars are out riding passengers around, then a passenger who wants a ride waits.

if a car is ready to load but there are no waiting passengers, then the car waits.

After every r units of time one passenger from the museum gets ready to take the safari car ride. 

Assume that Zoo is open for k units of time. Can you find the status of the passengers and safari cars after k units of time.

Input:
First line contains an integer N number of test cases. 
Each of the following N lines contains input data, separated by single space, for different test cases in the given order:

No. of Safari Cars
No. passengers in Museum at time zero
No. of passengers at zoo gate ready for ride at time zero.

p (When a car is available, it loads the one passenger it can hold and   rides the zoo for a specific amount of time say p. ),

r (After every r units of time one passenger from the museum gets ready to  take the safari car ride.) 

k (Zoo is open for k units of time.)


Output:
Each of n lines in the output has four integers separated by as space representing :

No. of cars waiting at the zoo gate
No. of passengers completed the zoo ride
No. of passengers wandering in the museum
No. of passengers still waiting to take a ride respectively 

#include <stdio.h>
#define min(A,B) ((A)>(B)?(B):(A))
#define max(A,B) ((A)>(B)?(A):(B))
int main(void)
{
  int testCount;
  scanf("%d", &testCount);
  while (testCount--){
  int cars, wander, ready, p, r, k;
  int doneCount, ridingCount, carsWaiting;
  int carArrives[50];
  int becomeReady[5100],nextCar,totalPeople,i;
  scanf("%d %d %d %d %d %d", &cars, &wander, &ready, &p, &r, &k);

  if (cars == 0){
    int movedToReady = min(wander, k/r);
    printf("0 0 %d %d\n", wander - movedToReady, ready + movedToReady);
    continue;
  }
  doneCount = ridingCount = 0;
  for (i = 0; i < cars; i++)
  carArrives[i] = 0;
  totalPeople = wander+ready;

  for (i = 0; i < ready; i++)
  becomeReady[i] = 0;

  for (i = ready; i < totalPeople; i++)
  becomeReady[i] = (i-ready+1)*r;

  nextCar = 0;

  for (i = 0; i < totalPeople; i++){
    int readyTime = becomeReady[i];
    if (readyTime > k)
    break;
    if (carArrives[nextCar] > readyTime)
    readyTime = carArrives[nextCar];
    carArrives[nextCar] = readyTime + p;
    nextCar = (nextCar+1) % cars;
    if (readyTime + p <= k)
    doneCount++;
    else if (readyTime <= k)
    ridingCount++;
  }
  carsWaiting = 0;

  for (i = 0; i < cars; i++)
  if (carArrives[i] <= k)
  carsWaiting++;
  printf("%d %d %d %d\n", carsWaiting, doneCount, max(0, wander - k/r), ready + min(wander, k/r) - doneCount - ridingCount);
  }
return 0;
}


INPUT_1:
3
4  33  6  5  2  14
6  21  4  2  2  18
5  17  4  8  5  2

OUTPUT:
0  8  26  1
5  12  12  0
3  6  13  0


INPUT_2:
2
17  165  15  9  7  12
21  152  11  7  5  10

OUTPUT:
16  15  164  0
19  11  150  0


ILLUSTRATION

eXECUTED using gcc

Morae Q!