다른 사람 해설보다 메모리를 1.5배 정도 더 사용한다.
어디가 문제일까
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
void input_test_case(vector<vector<int>>& test_case)
{
int i, j;
int T, N, M;
cin >> T;
for (i = 0; i < T; i++)
{
vector<int> temp;
cin >> N;
for (j = 0; j < N; j++)
{
cin >> M;
temp.push_back(M);
}
sort(temp.begin(), temp.end());
test_case.push_back(temp);
}
return;
}
void find_answer(vector<vector<int>>& test_case)
{
int i, j;
int max = 0, current, distance;
for (i = 0; i < test_case.size(); i++)
{
deque <int> test;
max = 0;
test.push_back(test_case[i][0]);
for (j = 1; j < test_case[i].size(); j++)
{
current = test_case[i][j];
if (j % 2 == 1)
{
distance = current - test.back();
distance = distance < 0 ? -(distance) : distance;
if (max < distance)
{
max = distance;
}
distance = current - test.front();
distance = distance < 0 ? -(distance) : distance;
if (max < distance)
{
max = distance;
}
test.push_back(current);
}
else
{
distance = current - test.back();
distance = distance < 0 ? -(distance) : distance;
if (max < distance)
{
max = distance;
}
distance = current - test.front();
distance = distance < 0 ? -(distance) : distance;
if (max < distance)
{
max = distance;
}
test.push_front(current);
}
}
cout << max << "\n";
}
return;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector<vector<int>> test_case;
input_test_case(test_case);
find_answer(test_case);
return 0;
}