示例#1
0
 def dijkstra():
     COST = [[1e4]*m for _ in range(n)]
     COST[0][0] = 0
     deque = [(0, 0)]
 
     while True:
         x, y = deque.pop(0) 
         if x == m-1 and y == n-1:
             return COST[n-1][m-1]
 
         cost = COST[y][x]
         for x, y in [(x, y-1), (x-1, y), (x+1, y), (x, y+1)]:
             if not (0 <= x < m and 0 <= y < n):
                 continue
 
             is_wall = status[y][x] == '1'
             new_cost = cost + (1 if is_wall else 0)
 
             if COST[y][x] <= new_cost:
                 continue
 
             COST[y][x] = new_cost
             if is_wall:
                 deque.append((x, y))
             else:
                 deque.insert(0, (x, y))
示例#2
0
#implement double ended queue.

from collections import deque

deque = deque()

print(deque)
print('Type of deque: ' + str(type(deque)))

print('Append.')
deque.append(1)
deque.appendleft(0)
deque.append(3)
print(deque)

#updating deque
print('Update.')
deque[2] = 2
#same as insert(i,a)
deque.insert(2, 2)
print(deque)

print('Pop left')
deque.popleft()
print(deque)

print('Pop')
deque.pop()
print(deque)
示例#3
0
#10866
#덱

from collections import deque
import sys
deque = deque()
for i in range(int(sys.stdin.readline())):
    tmp = sys.stdin.readline()
    if tmp[0:6] == 'push_f':
        deque.insert(0, tmp[11:len(tmp) - 1])
    if tmp[0:6] == 'push_b':
        deque.append(tmp[10:len(tmp) - 1])
    if tmp[0:5] == 'pop_f':
        if not deque:
            print(-1)
        else:
            print(deque.popleft())
    if tmp[0:5] == 'pop_b':
        if not deque:
            print(-1)
        else:
            print(deque.pop())
    if tmp[0] == 's':
        print(len(deque))
    if tmp[0] == 'e':
        if not deque:
            print(1)
        else:
            print(0)
    if tmp[0] == 'f':
        if not deque:
示例#4
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)
示例#5
0
from sys import stdin
from collections import deque
from math import floor
import time

N = 1000000
queue = [0 for i in range(N)]
deque = deque()
for i in range(N):
    deque.append(i)

# start = time.time()
# for i in range(10000):
#     del queue[1000]
# end = time.time()
# print(floor((end-start)*1000), "ms")

start = time.time()
for i in range(200000):
    deque.insert(10000, 1)
end = time.time()
print(floor((end-start)*1000), "ms")