4人黑夜1灯过一座桥,单人分别用时【1,3,6,8】分钟,桥上最多可以行2人,问最低用时多少分钟?
一思路 ,人按照时间排序,分别乘以1301再相加(五人23101)
二思路 最少时间的人来回送
三暴力搜索验证:
private static void testCrossBridge() {
System.out.println("testCrossBridge start");
int M = 11_000_000;
int[] re = new int[M];
boolean debug = false;
List<CrossResult> result = new ArrayList();
for (int r = 0; r < M; r++) {
List<Person> ps = new ArrayList<>();
ps.add( new Person("Ali",1));
ps.add(new Person("Bob", 3));
ps.add(new Person("Cua", 6));
ps.add(new Person("Dis", 8));
ps.add(new Person("Eva", 12));
// ps.add( new Person("Faa",9));
// ps.add( new Person("Gaa",11));
int allTime = 0;
int waitPerson = ps.size();
int count = 0;
// two in one boat
List<Person> crossStep = new ArrayList<>();
Random rand = new Random(System.currentTimeMillis());
List<Person> crossedList = new ArrayList<>();
while (waitPerson > 0) {
count ++;
if (waitPerson == 2) {
Person p1 = ps.get(0);
Person p2 = ps.get(1);
int time = ((p1.time > p2.time) ? p1.time : p2.time);
allTime += time;
if (debug) System.out.println("第"+count+"轮 last go : " + p1 + " " + p2 + ",time=" + time);
crossStep.add(p1);
crossStep.add(p2);
break;
}
int i = rand.nextInt(ps.size());
Person p1 = ps.get(i);
ps.remove(p1);
if (debug) System.out.println("ps.size " + ps.size() + ",waitPerson " + waitPerson);
int j = rand.nextInt(ps.size());
Person p2 = ps.get(j);
ps.remove(p2);
waitPerson -=2;
int time = ((p1.time > p2.time) ? p1.time : p2.time);
allTime += time;
if (debug) System.out.println("第"+count+"轮: i=" + i + " j=" + j + ",p1="+p1+",p2="+p2 +", time=" + time);
crossedList.add(p1);
crossedList.add(p2);
crossStep.add(p1);
crossStep.add(p2);
if (debug) System.out.print("crossed:");
for (int k = 0; k < crossedList.size(); k++) {
if (debug) System.out.print(crossedList.get(k) + " ");
}
if (debug) System.out.println();
Collections.sort(crossedList);
Person back = crossedList.get(0);
crossedList.remove(back);
ps.add(back);
waitPerson++;
allTime += back.time;
if (debug) System.out.println("back : " + back + ",ps size =" + ps.size());
if (debug) System.out.println("---------------------------");
}
if (debug) System.out.println("all time = " + allTime);
re[r] = allTime;
result.add(new CrossResult(allTime, crossStep));
}
// System.out.println(Arrays.toString(re));
// Collections.sort(result);
CrossResult minTime = result.stream().min(CrossResult::compareTo).get();
System.out.println("min : " +minTime);
// List aa = result.stream().filter(it -> it.allTime == minTime).collect(Collectors.toList());
// for (int i = 0; i < aa.size(); i++) {
// System.out.println(aa.get(i));
// }
}
public static class CrossResult implements Comparable {
public int allTime;
List<Person> steps;
public CrossResult(int allTime, List<Person> steps) {
this.allTime = allTime;
this.steps = steps;
}
@Override
public int compareTo(@NotNull Object o) {
return this.allTime - ((CrossResult) o).allTime;
}
@Override
public String toString() {
StringBuilder re = new StringBuilder();
re.append("time=");
re.append(allTime);
re.append(",[");
for (int i = 0; i < steps.size(); i +=2) {
re.append("[");
re.append(steps.get(i));
re.append(" ");
re.append(steps.get(i+1));
re.append("],");
}
re.append("]");
return re.toString();
}
}
public static class Person implements Comparable {
public String name;
public int time;
public Person(String name, int time) {
this.name = name;
this.time = time;
}
public Person() {
}
@Override
public String toString() {
return name + "-" + time + "分";
}
@Override
public int compareTo(@NotNull Object o) {
return this.time - ((Person) o).time;
}
}