Problem Statement

    

A well-known riddle goes like this: Four people are crossing an old bridge. The bridge cannot hold more than two people at once. It is dark, so they can't walk without a flashlight, and they only have one flashlight! Furthermore, the time needed to cross the bridge varies among the people in the group. For instance, let's say that the people take 1, 2, 5 and 10 minutes to cross the bridge. When people walk together, they always walk at the speed of the slowest person. It is impossible to toss the flashlight across the bridge, so one person always has to go back with the flashlight to the others. What is the minimum amount of time needed to get all the people across the bridge?

In this instance, the answer is 17. Person number 1 and 2 cross the bridge together, spending 2 minutes. Then person 1 goes back with the flashlight, spending an additional one minute. Then person 3 and 4 cross the bridge together, spending 10 minutes. Person 2 goes back with the flashlight (2 min), and person 1 and 2 cross the bridge together (2 min). This yields a total of 2+1+10+2+2 = 17 minutes spent.

You want to create a computer program to help you solve new instances of this problem. Given an int[] times, where the elements represent the time each person spends on a crossing, your program should return the minimum possible amount of time spent crossing the bridge.

Definition

    
Class: BridgeCrossing
Method: minTime
Parameters: int[]
Returns: int
Method signature: int minTime(int[] times)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 64

Notes

- In an optimal solution, exactly two people will be sent across the bridge with the flashlight each time (if possible), and exactly one person will be sent back with the flashlight each time. In other words, in an optimal solution, you will never send more than one person back from the far side at a time, and you will never send less than two people across to the far side each time (when possible).

Constraints

- times will have between 1 and 6 elements, inclusive.
- Each element of times will be between 1 and 100, inclusive.

Examples

0)  
    
{ 1, 2, 5, 10 }
Returns: 17
The example from the text.
1)  
    
{ 1, 2, 3, 4, 5 }
Returns: 16
One solution is: 1 and 2 cross together (2min), 1 goes back (1min), 4 and 5 cross together (5min), 2 goes back (2min), 1 and 3 cross together (3min), 1 goes back (1min), 1 and 2 cross together (2min). This yields a total of 2 + 1 + 5 + 2 + 3 + 1 + 2 = 16 minutes spent.
2)  
    
{ 100 }
Returns: 100
Only one person crosses the bridge once.
3)  
    
{ 1, 2, 3, 50, 99, 100 }
Returns: 162
 

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

开始时从right中取得最小值回到left使用Arrays.sort排序后选择rigth[0],显然会对数组顺序产生影响,导致后续遍历出错

 import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*; public class BridgeCrossing {
private int dfs(int[] left, int[] right) {
if (left.length <= 2) {
Arrays.sort(left);
return left[left.length - 1];
}
int min = Integer.MAX_VALUE;
for (int i = 0; i < left.length - 1; i++) {
for (int j = i + 1; j < left.length; j++) {
int[] lt = new int[left.length - 1];
int p = 0;
int[] rt = new int[right.length + 1];
int q = 0;
p = 0;
for (int k = 0; k < left.length; k++) {
if (k != i && k != j) {
lt[p] = left[k];
p++;
}
}
right[right.length - 1] = left[i];
right[right.length - 2] = left[j];
int tmp = left[i] > left[j] ? left[i] : left[j];
int lm = 0;
for (int k = 1; k < right.length; k++) {
if (right[k] < right[lm])
lm = k;
}
tmp += right[lm];
lt[p] = right[lm];
q = 0;
for (int k = 0; k < right.length; k++) {
if (lm != k) {
rt[q] = right[k];
q++;
}
} tmp += dfs(lt, rt);
if (tmp < min)
min = tmp;
}
}
return min;
} public int minTime(int[] times) {
return dfs(times, new int[2]);
}
}

SRM 146 DIV2 1000的更多相关文章

  1. Topcoder Srm 673 Div2 1000 BearPermutations2

    \(>Topcoder \space Srm \space 673 \space Div2 \space 1000 \space BearPermutations2<\) 题目大意 : 对 ...

  2. Topcoder Srm 671 Div2 1000 BearDestroysDiv2

    \(>Topcoder \space Srm \space 671 \space Div2 \space 1000 \space BearDestroysDiv2<\) 题目大意 : 有一 ...

  3. SRM 595 DIV2 1000

    数位DP的感觉,但是跟模版不是一个套路的,看的题解,代码好理解,但是确实难想. #include <cstdio> #include <cstring> #include &l ...

  4. TC SRM 593 DIV2 1000

    很棒的DP,不过没想出,看题解了..思维很重要. #include <iostream> #include <cstdio> #include <cstring> ...

  5. TC SRM 591 DIV2 1000

    很不错的一题,非常巧妙的用DP顺序解决这个问题... 可以发现,只和A里面最小的有关系... #include <cstdio> #include <cstring> #inc ...

  6. TopCoder SRM 660 Div2 Problem 1000 Powerit (积性函数)

    令$f(x) = x^{2^{k}-1}$,我们可以在$O(k)$的时间内求出$f(x)$. 如果对$1$到$n$都跑一遍这个求解过程,时间复杂度$O(kn)$,在规定时间内无法通过. 所以需要优化. ...

  7. TopCoder SRM 301 Div2 Problem 1000 CorrectingParenthesization(区间DP)

    题意  给定一个长度为偶数的字符串.这个字符串由三种括号组成. 现在要把这个字符串修改为一个符合括号完全匹配的字符串,改变一个括号的代价为$1$,求最小总代价. 区间DP.令$dp[i][j]$为把子 ...

  8. SRM 657 DIV2

    -------一直想打SRM,但是感觉Topcoder用起来太麻烦了.题目还是英文,不过没什么事干还是来打一打好了.但是刚注册的号只能打DIV2,反正我这么弱也只适合DIV2了.. T1: 题目大意: ...

  9. SRM 638 Div2

    2333... 因为TC过少的参与者.加上不断fst 我掉了div2该. 幸运的是完成的背div1该.. 250 水的问题 500 水的问题.. 直接bfs扩展即可了 注意判重.  我还用康托展开了真 ...

随机推荐

  1. 获取小众ftp服务器指定目录内容列表

    今天获取小众ftp服务器指定目录内容列表时费劲急了. ///parama url="ftp://x.x.x.x/dir_name" public string GetFTPDir( ...

  2. storm基础系列之一----storm并发度概念剖析

    前言: 学了几天storm的基础,发现如果有hadoop基础,再理解起概念来,容易的多.不过,涉及到一些独有的东西,如调度,如并发度,还是很麻烦.那么,从这一篇开始,力争清晰的梳理这些知识. 在正式学 ...

  3. 黄聪:CamtasiaStudio如何导出视频上传优酷实现高清

  4. Ubuntu开启22端口

    [http://blog.csdn.net/baple/article/details/39288817] 安装OpenSSH Ubuntu缺省没有安装SSH Server,使用以下命令安装: sud ...

  5. GridLookUpEdit多列模糊查询最简单方式 z

    GridLookUpEdit的知识库是RepositoryItemGridLookUpEdit,切确的说GridLookUpEdit只是RepositoryItemGridLookUpEdit的一个壳 ...

  6. C#如何获取CPU处理器核心数量 z

    有几条不同的处理器信息,您可以获得有关的信息:物理处理器数量.核心数量和逻辑处理器数量,这些可以不同.两颗双核超线程(启用)处理器的机器情况下有:2个物理处理器.4个核心和8个逻辑处理器. 逻辑处理器 ...

  7. 80、Android Support v4、v7、v13的区别以及应用场景

    一.简介 在 Android 开发中,为了使用高版本API的新特性,需要添加额外的包来使用这些新特性,这就是 Android Support 包 二.分类 Android Support v4: 这个 ...

  8. 1、Android Studio集成极光推送(Jpush) 报错 java.lang.UnsatisfiedLinkError: cn.jpush.android.service.PushProtoco

    Android studio 集成极光推送(Jpush) (华为手机)报错, E/JPush: [JPushGlobal] Get sdk version fail![获取sdk版本失败!] W/Sy ...

  9. WebStorm注册码

    WebStorm注册码User Name:EMBRACE License Key:===== LICENSE BEGIN =====24718-1204201000001h6wzKLpfo3gmjJ8 ...

  10. agile/scrum 如果一切都从解放前开始

    一个非常珍贵的机会,聚集了公司很多牛人,进行了一场发人深省的讨论.有一个话题我想拿出来和他家分享一下我的看法. 越来越不舒服的站会 站会是每天都在固定的时间.地点,大概持续15分钟左右(我们的小组都比 ...