-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_11497.cpp
More file actions
64 lines (53 loc) · 1.34 KB
/
BOJ_11497.cpp
File metadata and controls
64 lines (53 loc) · 1.34 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
#include <bits/stdc++.h>
using namespace std;
// 통나무의 높이를 오름차순으로 정렬 후,
// 왼쪽부터 오른쪽으로 하나씩 건너뛰며 배치하고(짝수 index),
// 다시 끝에서부터 왼쪽으로 하나씩 건너뛰며 배치한다(홀수 index)
// 마지막으로 인접한 두 개의 통나무 높이를 비교해가며 최대값을 찾는다.
int main() {
int T;
cin >> T;
for (int i = 0; i < T; i++) {
int N, input;
int result = -1;
vector<int> woods, sequence;
cin >> N;
for (int j = 0; j < N; j++) {
cin >> input;
woods.push_back(input);
}
sort(woods.begin(), woods.end());
if ((int)woods.size() % 2 == 0) {
for (int k = 0; k < (int)woods.size() / 2; k++) {
sequence.push_back(woods[k * 2]);
}
for (int k = 0; k < (int)woods.size() / 2; k++) {
sequence.push_back(woods[(int)woods.size() - 1 - (k * 2)]);
}
}
else {
for (int k = 0; k < ((int)woods.size() + 1) / 2 ; k++) {
sequence.push_back(woods[k * 2]);
}
for (int k = 0; k < ((int)woods.size() - 1) / 2; k++) {
sequence.push_back(woods[(int)woods.size() - 2 - (k * 2)]);
}
}
int a = 0;
int b = 1;
int gap, rotateGap;
while (b != sequence.size()) {
gap = abs(sequence[b] - sequence[a]);
if (gap > result) {
result = gap;
}
a++;
b++;
}
rotateGap = abs(sequence[a] - sequence[0]);
if (rotateGap > result) {
result = rotateGap;
}
cout << result << endl;
}
}