#GD0000194. Đưa mọi người qua cầu (Bridge)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đưa mọi người qua cầu (Bridge)
Bridge
Source: UVa
Version: Phuoc Hung OJ (single-case adaptation where applicable)
Problem Statement
There are people who must cross a bridge at night. Person needs seconds to cross. There is only one flashlight, and each crossing carries one or two people and the flashlight. Two people cross at the slower person’s time. The flashlight must be returned while people remain on the starting side. Find the minimum total time and output a sequence of crossings attaining it. People with equal crossing times are distinct, but are represented by their times, as in UVa.
Input
The first line contains . Exactly positive times follow, possibly one per line. There is no number of test cases and no blank-line separator.
Output
The first line prints the minimum time. Each following line lists one or two crossing times representing the people who travel with the flashlight from its current side. Directions alternate, starting from the original bank, and everyone must end on the far bank. Any optimal valid schedule is accepted.
Subtasks
- Subtask 1 (20 points): ; for every ; repeated times are allowed.
- Subtask 2 (30 points): ; for every ; repeated times are allowed.
- Subtask 3 (50 points): ; for every ; repeated times are allowed.
Examples
Example 1
Input
4
1
2
5
10
Output
17
1 2
1
5 10
2
1 2
Explanation
Send 1 and 2 across (2), return 1 (1), send 5 and 10 across (10), return 2 (2), and send 1 and 2 across (2). Total: 17.
Example 2
Input
4
1
1
2
2
Output
6
1 1
1
2 2
1
1 1
Explanation
Send both 1-time people across (1), one returns (1), send both 2-time people across (2), a 1-time person returns (1), then both 1-time people cross (1), totaling 6.