-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccc_2014_S3.py
49 lines (40 loc) · 1.41 KB
/
ccc_2014_S3.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
import sys
n = int(sys.stdin.readline())
for x in range(n):
m = int(sys.stdin.readline())
mountain = []
branch = []
result = "Y"
for x in range(m):
mountain.append(int(sys.stdin.readline()))
while mountain[len(mountain)-1] != 1:
branch.append(mountain[len(mountain)-1])
mountain.pop()
mountain.pop() #Getting rid of the "one" value and throwing it in the lake first will always work
need = 2
while len(mountain) > 0 or len(branch) > 0:
if len(branch) > 0 and len(mountain) > 0:
if branch[len(branch)-1] == need:
branch.pop()
need += 1
elif mountain[len(mountain)-1] == need:
mountain.pop()
need += 1
else: #We can't do anything else, so we shove the mountain into the branch
branch.append(mountain[len(mountain)-1])
mountain.pop()
elif len(branch) > 0: #If only the branch has elements in it
if branch[len(branch)-1] == need:
branch.pop()
need += 1
else: #The candies will not be put into the lake in order, so the result is no and we break
result = "N"
break
else: #If only the mountain has elements in it and nothing in the branch
if mountain[len(mountain)-1] == need:
mountain.pop()
need += 1
else:
branch.append(mountain[len(mountain)-1])
mountain.pop()
print (result)