forked from ajahuang/UVa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa 10037 - Bridge.cpp
88 lines (81 loc) · 1.9 KB
/
UVa 10037 - Bridge.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
void print(stringstream &ss, int t1, int t2 = -1)
{
if (t2 == -1)
ss << t1 << endl;
else
ss << t1 << " " << t2 << endl;
}
int main()
{
int T;
cin >> T;
while ( T-- )
{
int n;
cin >> n;
vector<int> t(n + 1);
for (int i = 1; i <= n; ++i)
cin >> t[i];
sort(t.begin(), t.end());
stringstream ss;
int c = n;
int totalTime = 0;
// In each round consider the fastest 2 people (A and B) and
// the slowest 2 people (Y and Z).
while (c >= 4)
{
// <AB> <A> <YZ> <B>
int t1 = t[2] + t[1] + t[c] + t[2];
// <AZ> <A> <AY> <A>
int t2 = t[c] + t[1] + t[c - 1] + t[1];
if (t1 < t2)
{
totalTime += t1;
print(ss, t[1], t[2]);
print(ss, t[1]);
print(ss, t[c - 1], t[c]);
print(ss, t[2]);
}
else
{
totalTime += t2;
print(ss, t[1], t[c]);
print(ss, t[1]);
print(ss, t[1], t[c - 1]);
print(ss, t[1]);
}
c -= 2;
}
// <AB> <A> <AC>
if (c == 3)
{
totalTime += t[2] + t[1] + t[3];
print(ss, t[1], t[2]);
print(ss, t[1]);
print(ss, t[1], t[3]);
}
// <AB>
else if (c == 2)
{
totalTime += t[2];
print(ss, t[1], t[2]);
}
// <A>
else
{
totalTime += t[1];
print(ss, t[1]);
}
cout << totalTime << endl;
cout << ss.str();
if (T)
cout << endl;
}
return 0;
}