-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5672.cpp
More file actions
89 lines (85 loc) · 1.12 KB
/
5672.cpp
File metadata and controls
89 lines (85 loc) · 1.12 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
// 5672. 올해의 조련사
// 2019.07.24
#include<iostream>
#include<algorithm>
#include<queue>
#include<string>
using namespace std;
int n;
deque<char> dq;
string ans = "";
void go(int left, int right)
{
left++;
right--;
if (left > right)
{
while (1)
{
ans += dq.front();
dq.pop_front();
if (dq.empty())
{
break;
}
ans += dq.back();
dq.pop_back();
if (dq.empty())
{
break;
}
}
return;
}
if (dq[left] < dq[right])
{
ans += dq.front();
dq.pop_front();
return;
}
else if (dq[right] < dq[left])
{
ans += dq.back();
dq.pop_back();
return;
}
else
{
go(left, right);
}
}
int main()
{
int t;
cin >> t;
for (int testCase = 1; testCase <= t; testCase++)
{
cin >> n;
dq.clear();
dq.resize(n);
for (int i = 0; i < n; i++)
{
cin >> dq[i];
}
ans = "";
while (!dq.empty())
{
if (dq.back() < dq.front())
{
ans += dq.back();
dq.pop_back();
}
else if (dq.front() < dq.back())
{
ans += dq.front();
dq.pop_front();
}
else
{
go(0, dq.size() - 1);
}
}
cout << "#" << testCase << " " << ans << endl;
}
return 0;
}