-
Notifications
You must be signed in to change notification settings - Fork 15
/
1055.cpp
64 lines (58 loc) · 1.55 KB
/
1055.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
/*
greedy > loading balance
difficulty: medium
date: 24/Jan/2020
by: @brpapa
*/
#include <cmath>
#include <deque>
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T; cin >> T;
for (int t = 1; t <= T; t++) {
int a[52], N; cin >> N;
for (int i = 0; i < N; i++) cin >> a[i];
sort(a, a+N);
deque<int> A(a, a+N);
int prevFront = A.front(); A.pop_front();
int prevBack = A.back(); A.pop_back();
int ans = abs(prevBack-prevFront);
while (!A.empty()) {
int dif[4] = {
abs(prevBack - A.front()),
abs(prevBack - A.back()),
abs(prevFront - A.back()),
abs(prevFront - A.front())
};
if (max(dif[0], dif[1]) > max(dif[2], dif[3])) {
if (dif[0] > dif[1]) {
ans += abs(prevBack - A.front());
prevBack = A.front();
A.pop_front();
}
else {
ans += abs(prevBack - A.back());
prevBack = A.back();
A.pop_back();
}
}
else {
if (dif[2] > dif[3]) {
ans += abs(prevFront - A.back());
prevFront = A.back();
A.pop_back();
}
else {
ans += abs(prevFront - A.front());
prevFront = A.front();
A.pop_front();
}
}
}
printf("Case %d: %d\n", t, ans);
}
return 0;
}