-
Notifications
You must be signed in to change notification settings - Fork 1
/
piling_up.py
52 lines (50 loc) · 1.71 KB
/
piling_up.py
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
# Author: Bojan G. Kalicanin
# Date: 19-Nov-2016
# There is a horizontal row of n cubes. The length of each cube is
# given. You need to create a new vertical pile of cubes. The new pile
# should follow these directions: if cubeI is on top of cubeJ then
# sideLengthJ >= sideLengthI.
#
# When stacking the cubes, you can only pick up either the leftmost or
# the rightmost cube each time. Print "Yes" if it is possible to stack
# the cubes. Otherwise, print "No". Do not print the quotation marks.
from collections import deque
t = int(input()) # number of test cases
for i in range(t):
n = int(input())
sideLengths = deque()
while len(sideLengths) < n:
sideLengths.extend(map(int, input().split()))
l = list()
flag = True
while sideLengths and flag:
left = sideLengths[0]
if len(sideLengths) > 1:
right = sideLengths[-1]
if len(l) > 0:
if left >= right and left <= l[-1]:
left = sideLengths.popleft()
l.append(left)
elif right > left and right <= l[-1]:
right = sideLengths.pop()
l.append(right)
else:
flag = False
else:
if left >= right:
left = sideLengths.popleft()
l.append(left)
else:
right = sideLengths.pop()
l.append(right)
else:
if len(l) > 0:
if left <= l[-1]:
left = sideLengths.popleft()
l.append(left)
else:
flag = False
if flag:
print('Yes')
else:
print('No')