快速过桥 详解

[问题描述]

A group of n people wish to cross a bridge at night.
n个人的队伍想在晚上通过一座大桥。

At most two people may cross at any time, and each group must have a flashlight.
任何时间最多有2人通过,每组必须有一个手电筒。

Only one flashlight is available among the n people, so some sort of shuttle arrangement must be arranged in order to return the flashlight so that more people may cross.
很可怜,这n个人只有一个手电筒可用,因此必须合理地安排,让手电筒能回到另一端,这样才能让更多人通过大桥。

Each person has a different crossing speed; the speed of a group is determined by the speed of the slower member.
每个人有不同的速度,编组后,一个组的速度等于慢的那个人的速度。

Your job is to determine a strategy that gets all n people across the bridge in the minimum time.
你的任务是实现一种策略,让所有人在最短时间内通过。

[输入]
The input begins with a single positive integer on a line by itself indicating the number of test cases, followed by a blank line. There is also a blank line between each two consecutive inputs.
第一行是一个单独的数字,代表测试案例的个数;随后是一个空行。后面每两个输入之间都有一个空行。

The first line of each case contains n, followed by n lines giving the crossing times for each of the people. There are not more than 1,000 people and nobody takes more than 100 seconds to cross the bridge.
每个测试案例的第一行是n的值,随后n行是每个人单独通过大桥的时间。人数≤1000,时间≤100s

【输出】
For each test case, the first line of output must report the total number of seconds required for all n people to cross the bridge.
对每个案例,首行是n个人通过的时间。

Subsequent lines give a strategy for achieving this time. Each line contains either one or two integers, indicating which person or people form the next group to cross.
随后是你的通过和返回次序,每一行是一个或者两个整数,对应着人(通过时间代表人)。返回也要占一行。

Each person is indicated by the crossing time specified in the input.Although many people may have the same crossing time, this ambiguity is of no consequence.
虽然不同的人可能有相同的速度,但这没有影响(看做一种人就好了)。

Note that the crossings alternate directions, as it is necessary to return the flashlight so that more may cross.
注意方向,意思就是说返回情况也要占一行。

If more than one strategy yields the minimal time, any one will do.
多种方案都是最短时间的,任选其一皆可。

The output of two consecutive cases must be separated by a blank line.
案例之间空行分界。

【样例输入】

1

4
1
2
5
10

【样例输出】

17
1 2
1
5 10
2
1 2

【解释】
17 秒
1 2 2秒
1 1秒
5 10 10秒
2 2秒
1 2 2秒

破解思路

首先将所有过桥时间排序,以下为获得最小过桥时间的算法:

  • A 过桥总人数为 1,该人的过桥时间即为最短过桥时间。

  • B 过桥总人数为 2,过桥时间较大的人的过桥时间即为最短过桥时间。

  • C 过桥总人数为 3,假设为A,B,C,则(AB),(A),(AC)策略和 (AC),(A),(AB)策略的时间相同,3人过桥时间之和为最短时间。

  • D 过桥总人数大于3,假设最前面为A,B两人,最后为Y,Z两人,(A < B < Y < Z)有两种策略: (一种是 1 2 8 9 这样的,前两个数都比较小,第一次都过去的话,1 和 2 依次返回送手电筒的代价都很小,比 8 和 9返回的代价小多了,还有一种是 1 8 9 10 这种的,会发现让 8 返回送手电筒的代价也很大,这种时候就可以让 1 一直往返送手电筒,开销最小,至于采用哪种方式可以通过2 x B + A + Z 与 2 x A + B +Y + Z 做一下对比)

    • (AB),(A),(YZ),(B),(AB) AB先走,AB依次返回
    • (AB),(A),(AY),(A),(AZ) AB先走,只有A返回

比较两种策略那种过桥时间少就选那种,这个时候两个人过去了,将总人数减去 2,继续执行上述程序,若总人数仍大于 3,继续该步骤直到剩下需要过桥的人数小于等于 3。
可用递归或直接迭代实现。

可以总结出不管是那种情况,AB都可以先走,只不过根据判断出的结果将后续的四个存入队列就好了。

不太通顺的理解,这是我通过执行代码的过程得出的理解思路 这个题目可以看做是只把前两个数当做是运输的工具,持续进入递归,直到至少将前三个数排入队列后,在向后移动两个数,直到结束 仅供参考

解题代码

import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int M = sc.nextInt();
		sc.nextLine();
		for (int i = 0; i < M; i++) {
			int n = sc.nextInt();
			int[] data = new int[n];
			for (int j = 0; j < n; j++) {
				data[j] = sc.nextInt();
			}
			Arrays.sort(data);
			TimeAndPath res = deal(data, 0, data.length - 1);
			res.print();
			if (i < M - 1)// 最后一个空行,UVA的空行也严格比对
				System.out.println();
		}
	}

	private static TimeAndPath deal(int[] data, int begin, int end) {
		TimeAndPath res = new TimeAndPath();
		int A = data[begin];
		if (end == begin) {// one person
			res.time = A;
			res.q.add(String.valueOf(A));
			return res;
		}
		int Z = data[end];
		if (end == begin + 1) {// tow persons
			res.time = Z;
			res.q.add(A + " " + Z);
			return res;
		}
		int B = data[begin + 1];
		if (end == begin + 2) {// three persons
			res.time = A + B + Z;
			res.q.add(A + " " + B);
			res.q.add(String.valueOf(A));
			res.q.add(A + " " + Z);
			return res;
		}
		int Y = data[end - 1];
		// 有两个走的,处理剩余部分,递归到不大于三个后,跳出递归,相当于依次向后遍历两个数
		res = deal(data, begin, end - 2);
		if (A + 2 * B + Z <= 2 * A + Z + Y) {// A,B first 依次返回	
			// 加上当前这个步骤的时间
			res.time += A + 2 * B + Z;
			// 添加当前这个步骤的路径
			res.q.add(B + "");
			res.q.add(Y + " " + Z);
			res.q.add(A + "");
			res.q.add(A + " " + B);			
		} else {// A,B first,A持续返回
			// 加上当前这个步骤的时间
			res.time += 2 * A + Z + Y;
			// 添加当前这个步骤的路径	
			res.q.add(A + "");
			res.q.add(A + " " + Y);
			res.q.add(A + "");
			res.q.add(A + " " + Z);	
		}
		return res;
	}

	/**
	 * 因为要同时关注时间和路径,不太好处理,就合成一个对象
	 */
	private static class TimeAndPath {
		int time;
		Deque<String> q = new LinkedList<>();

		void print() {
			System.out.println(time);
			for (String p : q) {
				System.out.println(p);
			}
		}
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值