Skip to content

Instantly share code, notes, and snippets.

@hernantz
Created April 23, 2016 14:53
Show Gist options
  • Save hernantz/6d49ec91abd0b185f0629d50c137b745 to your computer and use it in GitHub Desktop.
Save hernantz/6d49ec91abd0b185f0629d50c137b745 to your computer and use it in GitHub Desktop.
from collections import deque
def sol(inp):
strd = sorted(inp)
enum = enumerate(strd)
for idx, item in enum:
# ASEGURARSE QUE ESTO SEA UN TRIO SIEMPRE
next_3 = idx + 3
tri_inp = inp[idx:next_3]
tri_strd = strd[idx:next_3]
if item == inp[idx] and len(strd[idx:]) != 3:
continue
d = deque(tri_inp)
d.rotate()
first_rot = list(d)
d.rotate()
second_rot = list(d)
if tri_strd not in [tri_inp, first_rot, second_rot]:
break
if inp[idx + 3:] and (inp[next_3:] != strd[next_3:]):
break
try:
[next(enum) for x in tri_inp]
except StopIteration:
continue
else:
return "YES"
return "NO"
if __name__ == '__main__':
num_tests = int(raw_input())
for i in range(num_tests):
_ = raw_input()
inp = map(int, raw_input().split(' '))
print sol(inp)
assert sol([1, 4, 3, 2, 5]) == "NO"
assert sol([1, 2, 3]) == "YES"
assert sol([3, 1, 2]) == "YES"
assert sol([2, 1, 3]) == "NO"
assert sol([1, 3, 4, 2]) == "YES"
assert sol([1, 2, 3, 4, 5]) == "YES"
assert sol([1, 3, 2, 4, 5]) == "NO"
assert sol([3, 4, 5, 2, 1]) == "NO"
assert sol([3, 1, 2, 4, 5]) == "YES"
assert sol([1, 2, 3, 5, 4]) == "NO"
assert sol([1, 2, 3, 5]) == "YES"
assert sol([1, 3, 2, 4]) == "NO"
assert sol([3, 2, 1, 4]) == "NO"
assert sol([2, 3, 1, 4]) == "YES"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment