Automatic Fueling Robot
C/C++.JAVA 3S
There are N cars parked in a row in a parking lot.
On both sides of the parking garage, as it is demonstrated in the picture below, there is a Gasoline and Diesel fueling station installed.
An automatic fueling robot carries the fuel form the station and fills up the parked cars with fuel.
The cars are divided into two types depending on whether it is a gasoline or Diesel car.
The automatic robot will be used to provide a cost-free fueling service which is filling up all cars with 1L of each corresponding fuel.
The robot will move between the two fueling stations as follows.
- The robot carries 2L of gasoline at the Gasoline station and starts moving from there.
- The robot can fill up the cars of the same type of gas it carries 1L each.
- The robot can go back to the fueling, it gets 2L of supply of the corresponding fuel (if the robot has some remaining fuel, it will be discarded.)
- There is an equal distance of 1 between each fueling station and the cars.
The fuel type of N cars parked in the parking lot will be given.
Find the minimum moving distance of the automatic fueling robot after it has filled up all cars with 1 fuel each.
[Example 1]
Given the number of total cars N=5 and the order of the parked cars such as G –D – G –D – G, the process of finding the minimum moving distance for fueling all cars is as follows:
In this case, the total moving distance until the last car is filled up is 12 which is the minimum moving distance.
[Example 2]
Given the total number of cars N=5 and the order of the parked cars such as D- G- G- D- G, the process of finding the minimum moving distance for fueling all cars is as shown below.
In this case, the total moving distance until the last car is filled up with fuel is 14 which is the minimum moving distance.
[Constrains]
- The number of total cars N is greater than or equal to 2 and less than or equal to 8.(2<=N<=8)
- The amount of fuel that the robot can carry all at once at a fueling station is 2L.
- All cars can get filled up with 1L of its corresponding fuel.
- There is no empty space between each parked car and they are always 1 distance apart.
- The tow fueling stations are 1 distance apart each from the outer parked car.
[Input]
The first line contains a single integer T- the number of total test cases.
From the next line on, each test case is given.
The first line of each case contains N-the number of total cars.
On the following line, N fuel information of the parked cars will be given as either 1 or 2. (1: Gasoline, 2: Diesel)
[Output]
Print “#t” (without the quotes), leave a blank space and print the answer. ( t refers to the test case number and starts with 1).
Print minimum moving distance of the automatic fueling robot after it has filled up all cars with 1L fuel each.
[Input/ Output Example]
Input Example |
5 3 2 1 2 5 1 2 1 2 1 5 2 1 1 2 1 8 2 2 1 1 1 2 1 1 8 2 2 2 2 2 2 2 2 |
Output Example |
#1 7 #2 12 #3 14 #4 35 #5 41 |