20160921 adv Automatic Fueling Robot 自动加油机器人

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.

  1. The robot carries 2L of gasoline at the Gasoline station and starts moving from there.
  2. The robot can fill up the cars of the same type of gas it carries 1L each.
  3. 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.)
  4. 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]

  1. The number of total cars N is greater than or equal to 2 and less than or equal to 8.(2<=N<=8)
  2. The amount of fuel that the robot can carry all at once at a fueling station is 2L.
  3. All cars can get filled up with 1L of its corresponding fuel.
  4. There is no empty space between each parked car and they are always 1 distance apart.
  5. 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

  • 41
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值