快速过桥

[问题描述]

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.

你的任务是实现一种策略,让所有人在最短时间内通过。

【输出】

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秒

#include<stdio.h>

  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];
    if (A + 2 * B + Z <= 2 * A + Z + Y) {//A,B first
      res = deal(data,begin,end-2);//处理剩余部分
      res.time+= A + 2 * B + Z;//加上当前这个步骤的时间
      //添加当前这个步骤的路径
      res.q.addFirst(B +"");
      res.q.addFirst(Y +" "+ Z);
      res.q.addFirst(A +"");
      res.q.addFirst(A +" "+ B);
    } else {//A,Z first
      res = deal(data,begin,end-2);//处理剩余部分
      res.time+=2 * A + Z + Y;//加上当前这个步骤的时间
      //添加当前这个步骤的路径
      res.q.addFirst(A +"");
      res.q.addFirst(A +" "+ Z);
      res.q.addFirst(A +"");
      res.q.addFirst(A +" "+ Y);
    }
    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);
      }
    }
  }

【解题思路】

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

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

 

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

 

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

 

(4)过桥总人数大于3,假设最前面为A,B两人,最后为Y,Z两人,有两种策略:(AB),(A),(YZ),(B),(AB)和(AZ),(A),(AY),(A),(AB)。

 

比较两种策略那种过桥时间少就选那种,然后将总人数减去 2,若总人数仍大于 3,继续该步骤直到剩下需要过桥的人数小于等于 3。

可用递归或直接迭代实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值