ABC066 C(AtCoder Beginner Contest)

takkii
Music and Technology
1 min readAug 25, 2019

Plan

In an operation,
first we append a_i to the end of b,
and second we reverse the order of the elements in b.
We repeat this operation for n times.

To Simplify these operations,
first append a_1 to a_n from right and left alternately,
second if n is odd number, reverse the order of the elements in b.

To Implement this,
we can use collections.deque.
We can insert a_i from left in O(1).

So in this task,
Time complexity is O(n)
(append a_1 to a_n in O(n),
reverse b in O(n)?)

Code

from collections import dequeN = int(input())
a_list = list(map(int,input().split()))
b = deque()
for i, a in enumerate(a_list):
if i % 2 == 0:
b.append(a)
else:
b.appendleft(a)
if N % 2 == 1:
b.reverse()
print(" ".join(map(str,b)))

--

--

takkii
Music and Technology

Competitive Programming, MachineLearning, Manga, Music, BoardGame.