题目
Description
n people wish to cross a bridge at night. A group of at most two people may cross at any time, and each group must have a flashlight. 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.
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.
Input
The first line of input contains n, followed by n lines giving the crossing times for each of the people. There are not more than 1000 people and nobody takes more than 100 seconds to cross the bridge.
Output
The first line of output must contain the total number of seconds required for all n people to cross the bridge. The following 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 the 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.
Sample Input
4
1
2
5
10
Sample Output
17
1 2
1
5 10
2
1 2
Source
Waterloo local 2000.09.30
原题传送门
原题传送门(vjudge)
思路
传说中的智力冲浪?
首先,
n
n
n个人,肯定每次过
2
2
2 个人,再一个人把灯送回来。
设当前序列
A
A
A 是最快的人,
B
B
B 是次快的人,
a
a
a 是最慢的人,
b
b
b 是次慢的人
让
a
a
a 和
b
b
b 用最少时间过桥,有两种过桥方案:
方案1:用最快的成员
A
A
A 传递手电筒,帮助a和b过桥,显然,
A
A
A 带
a
a
a 和
b
b
b 过桥,所用的时间是
2
∗
A
+
a
+
b
2*A+a+b
2∗A+a+b。
方案2:用最快的成员
A
A
A 和次快的成员
B
B
B 传递手电筒帮助a和b过桥,总的需要时间为
2
∗
B
+
A
+
a
2*B+A+a
2∗B+A+a。
比较哪种更快。
我们每次都帮助最慢和次慢的两个人过桥,最后可能有两种情况。
n
=
=
2
n == 2
n==2 :全部过桥,加上
B
B
B。
n
=
=
3
n == 3
n==3 :最快的人带着最慢的先过桥,再回来,然后再和次慢的过桥,加上
a
+
A
+
b
a+A+b
a+A+b。
Code 见㊦ ↓↓↓↓↓
代码
#include <cstdio>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <map>
#include <list>
#include <queue>
#include <istream>
#include <ostream>
#include <set>
#include <stack>
#include <cwctype>
#include <fstream>
#include <vector>
#include <cwchar>
#include <functional>
using namespace std;
int a[10100], n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
if (n == 1) printf("%d\n%d", a[1], a[1]), exit(0);
sort(a+1, a+1+n);
int ans = 0, tmp = n;
for ( ; n > 3; n -= 2)
if (a[1]+a[n-1] > a[2]+a[2]) ans += a[1]+a[2]+a[2]+a[n];
else ans += a[1]+a[1]+a[n-1]+a[n];
ans += (n == 2) ? a[2] : a[1]+a[2]+a[3];
printf("%d\n", ans); n = tmp;
for ( ; n > 3; n -= 2)
if (a[2]+a[2] < a[1]+a[n-1]) printf("%d %d\n%d\n%d %d\n%d\n", a[1], a[2], a[1], a[n-1], a[n], a[2]);
else printf("%d %d\n%d\n%d %d\n%d\n", a[1], a[n] ,a[1], a[1], a[n-1], a[1]);
if (n == 2) printf("%d %d", a[1], a[2]);
else printf("%d %d\n%d\n%d %d", a[1], a[2], a[1], a[1], a[3]);
return 0;
}
(不用在意头文件 😃 )