Ejemplo n.º 1
0
def synch_deque(deque, item, strict=None):
    # step or Point
    tmp = deque[0]
    if isinstance(tmp, tuple):
        # use step
        if not isinstance(item, tuple):
            # uzko!!!! [0]
            item = list(filter(lambda s: s[0] == item, deque))[0]
    else:
        # use point
        if not isinstance(item, Point):
            item = item[0]

    if item in deque:
        ind = deque.index(item)
        if -1 <= ind <= 1:
            deque.rotate(-ind)
    elif strict is None:
        pass
    elif strict:
        raise Exception(f'{item} is not contains in deque')
    else:
        # relevante rotate
        pass
Ejemplo n.º 2
0
from collections import deque

n, m = map(int, input().split())
out = map(int, input().split()) # not list type

deque = deque([i for i in range(1, n+1)])
result = 0

for i in out:
    left = deque.index(i)
    right = len(deque) - left
    print(left, right)
    if left < right:
        result += left
        deque.rotate(-left)
        deque.popleft()
    else:
        result += right
        deque.rotate(right)
        deque.popleft()
    print(result)
    
print("result = ", result)
Ejemplo n.º 3
0
from collections import deque

n, m = map(int, input().split(" "))
answer = list(map(int, input().split(" ")))
deque = deque()

for i in range(n):
    deque.append(i + 1)

count = 0

while len(answer) > 0:
    if answer[0] == deque[0]:
        answer.remove(answer[0])
        deque.popleft()

    elif deque.index(answer[0]) <= (len(deque) // 2):
        deque.append(deque[0])
        deque.popleft()
        count += 1

    elif deque.index(answer[0]) > (len(deque) // 2):
        deque.insert(0, deque[len(deque) - 1])
        deque.pop()
        count += 1

print(count)
Ejemplo n.º 4
0
print('Extend function.')
deque.extend('456')
deque.extend([7,8,9])
print(deque)

print('Append function.')
deque.append('abc')
deque.append(['d','e','f'])
print(deque)


#other functions.

print('Search index.')
# index(ele, beg, end)
print(deque.index(7,5,9))

print('Count')
print(deque.count(0))

print('Extend Left')
deque.extendleft([0])
print(deque)

print('Remove first occurence.')
deque.remove('abc')
print(deque)

print('Rotate by 2.')
deque.rotate(2)
print(deque)
Ejemplo n.º 5
0
# 문제 출처 : https://www.acmicpc.net/problem/1021

from sys import stdin
from collections import deque

n, p = map(int, stdin.readline().split())
arr = list(map(int, stdin.readline().split()))
deque = deque([i for i in range(1, n+1)])

count = 0

for num in arr:
    if deque[0] == num:
        deque.popleft()
        continue

    left_move = deque.index(num)
    right_move = len(deque) - left_move

    if left_move <= right_move:
        deque.rotate(-left_move)
        deque.popleft()
        count += left_move
    else:
        deque.rotate(right_move)
        deque.popleft()
        count += right_move

print(count)