当前位置:   article > 正文

蓝桥杯练习第六周-----快速过桥

蓝桥杯练习第六周-----快速过桥

一、[问题描述]
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秒
二、分析

  输入人数n,对其过桥时间并进行排序,  如果n==1,则直接过去;如果n==2,两人一起过去,时间为大的那个;如果n==3,先时间短的两人过去,时间最短的返回,之后一起过去;当n>3时,循环比较每次都让速度最快的送与最小耗时两个交替送的时间,选出耗时最小的。

三、代码

  1. public class Lanqiao06{
  2. public static void main(String[] args) {
  3. Scanner scan = new Scanner(System.in);
  4. while (scan.hasNext()) {
  5. int geshu = scan.nextInt();
  6. scan.nextLine();
  7. for (int gs = 0; gs < geshu; gs++) {
  8. int n = scan.nextInt();
  9. int[] data = new int[n];
  10. for (int i = 0; i < n; i++) {// 输入
  11. int innerdata = scan.nextInt();
  12. data[i] = innerdata;
  13. }
  14. for (int i = 0; i < data.length - 1; i++) {// 排序
  15. if (data[i] > data[i + 1]) {
  16. int temp = data[i];
  17. data[i] = data[i + 1];
  18. data[i + 1] = temp;
  19. }
  20. }
  21. int sum = 0;
  22. if (n == 1) {
  23. sum += data[0];
  24. System.out.println(sum);
  25. System.out.println(data[0]);
  26. }
  27. if (n == 2) {
  28. sum += data[0] + data[1];
  29. System.out.println(sum);
  30. System.out.println(data[0] + " " + data[1]);
  31. }
  32. if (n == 3) {
  33. sum += data[0] + data[1] + data[2];
  34. System.out.println(sum);
  35. System.out.println(data[0] + " " + data[1]);
  36. System.out.println(data[0]);
  37. System.out.println(data[0] + " " + data[2]);
  38. }
  39. if (n > 3) {
  40. sum += data[1];
  41. for (int i = 2; i < n; i += 2) {
  42. int m1 = data[i] + data[i + 1] + 2 * data[0];
  43. int m2 = data[i + 1] + 2 * data[1] + data[0];
  44. if (m1 > m2) {
  45. sum += m2;
  46. System.out.println("sum:" + sum);
  47. System.out.println(data[0] + " " + data[1]);
  48. System.out.println(data[0]);
  49. System.out.println(data[i] + " " + data[i + 1]);
  50. System.out.println(data[1]);
  51. System.out.println(data[0] + " " + data[1]);
  52. } else {
  53. if (i == 0)
  54. sum += m1;
  55. System.out.println("sum:" + sum);
  56. System.out.println(data[0] + " " + data[1]);
  57. System.out.println(data[0]);
  58. System.out.println(data[0] + " " + data[i]);
  59. System.out.println(data[0]);
  60. System.out.println(data[0] + " " + data[i + 1]);
  61. System.out.println(data[0]);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }

四、截图

样例

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/929640
推荐阅读
相关标签
  

闽ICP备14008679号