OA | Traffic Signal Management

There is a crossroad where N roads meet. Due to heavy traffic, managing traffic signals is quite difficult. On all N roads, there are vehicles waiting for the green light. You are given a two-dimensional array A where the 4 denotes a list of 4 elements containing road number, the number of two- wheelers on road, the number of three-wheelers on road, and the number of four-wheelers on road waiting for the traffic signal to turn green. In one second, 3 two-wheelers or 1 two- wheeler and 1 four-wheeler or two-wheeler and 1 three- wheeler or less than that can cross a road. All roads are of the same kind.

You have to manage the traffic signals and decide when to give the green signal on the roads. The rules for traffic signals are:

  • Once a green signal is given to a road, all the traffic on that particular road should clear the road by taking some amount of time that is mentioned
  • Only one green signal should be given at a time and should wait for all the traffic to go before the next green signal
  • The waiting time of traffic on a road equals the time when a road gets the green signal as everyone is waiting for the green signal from the 0 second.

Note: None of the roads are empty.

Find out in which sequence the green signal should be given and when it should be given to the roads in order to minimize the sum of waiting time over all roads. If there are several sequences having the same sum of waiting times, determine the lexicographically minimum.

Comments (1)