-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10037.cpp
More file actions
105 lines (98 loc) · 3.21 KB
/
Copy path10037.cpp
File metadata and controls
105 lines (98 loc) · 3.21 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Author: btjanaka (Bryon Tjanaka)
// Problem: (UVa) 10037
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORe(i, a, b) for (int i = a; i <= b; ++i)
#define PAI(arr, len) /*Print array of integers*/ \
{ \
for (int _i = 0; _i < len; ++_i) { \
if (_i != len - 1) { \
printf("%d ", arr[_i]); \
} else { \
printf("%d", arr[_i]); \
} \
} \
putchar('\n'); \
}
#define PBS(n, len) /*Print a bitset*/ \
{ \
for (int _i = 0; _i < len; ++_i) { \
putchar(n % 2 + '0'); \
n /= 2; \
} \
putchar('\n'); \
}
#define GET(x) scanf("%d", &x)
#define PLN putchar('\n')
#define INF 2147483647
typedef long long ll;
using namespace std;
int ppl[1010];
pair<int, int> data[10000];
int main() {
int ca;
GET(ca);
while (ca--) {
int n;
GET(n);
FOR(i, 0, n) { GET(ppl[i]); }
sort(ppl, ppl + n);
// See
// https://codingstrife.wordpress.com/2013/07/23/solution-uva10037-pc110403-bridge/
// for algorithm
int tot = 0;
int d_i = 0;
// Move all the slowest members over
// In each case, ppl[0] and ppl[1] start on the uncrossed side. ppl[i] and
// ppl[i + 1] are the slow members that need to cross.
// Method 1: fastest person does all the moving
// Method 2: fastest and next fastest work together
int limit = (n % 2 == 0) ? 2 : 3;
for (int i = n - 2; i >= limit; i -= 2) {
int method1 = ppl[i] + ppl[0] + ppl[i + 1] + ppl[0];
int method2 = ppl[1] + ppl[0] + ppl[i + 1] + ppl[1];
if (method1 < method2) {
tot += method1;
data[d_i++] = make_pair(ppl[0], ppl[i]);
data[d_i++] = make_pair(ppl[0], -1);
data[d_i++] = make_pair(ppl[0], ppl[i + 1]);
data[d_i++] = make_pair(ppl[0], -1);
} else {
tot += method2;
data[d_i++] = make_pair(ppl[0], ppl[1]);
data[d_i++] = make_pair(ppl[0], -1);
data[d_i++] = make_pair(ppl[i], ppl[i + 1]);
data[d_i++] = make_pair(ppl[1], -1);
}
n -= 2;
}
// There should now be 1, 2, or 3 members left, and they should all be the
// fastest of the original group. There is only one optimal crossing for
// each of these cases.
switch (n) {
case 1:
data[d_i++] = make_pair(ppl[0], -1);
tot += ppl[0];
break;
case 2:
data[d_i++] = make_pair(ppl[0], ppl[1]);
tot += ppl[1];
break;
case 3:
data[d_i++] = make_pair(ppl[0], ppl[1]);
data[d_i++] = make_pair(ppl[0], -1);
data[d_i++] = make_pair(ppl[0], ppl[2]);
tot += ppl[2] + ppl[0] + ppl[1];
break;
}
// Output
printf("%d\n", tot);
FOR(i, 0, d_i) {
printf("%d", data[i].first);
if (data[i].second != -1) printf(" %d", data[i].second);
PLN;
}
if (ca) PLN;
}
return 0;
}