def main(): n, q, k = [int(x) for x in input().strip().split()] arr = [str(x) for x in input().strip().split()] queryString = list(str(input())) arrString = makeString(arr) seq = findSequences(arrString) seq1 = seq[:] seq1.sort(reverse=True) #descending seq2 = seq[:] max1, max2 = seq1[0], seq1[ 0] #storing first and second maximum sequences' lengths if (len(seq) > 1): max2 = seq1[1] #storing second max if it is distinct seq2 = seq2[::-1] table = {} #used for caching state = 0 ind1 = len(seq2) - seq2.index( max1 ) - 1 #index of first occurance of max1 in seq2 which is in non-increasing order items = deq(arr) #queue itemLens = deq(seq) #queue l = len(itemLens) table[0] = adjust(max1, k) maxUpdates = min(n, queryString.count('!')) while (maxUpdates > 0): maxUpdates -= 1 state = state + 1 items.rotate() if (items[0] == '1'): itemLens[0] += 1 itemLens[l - 1] -= 1 if (ind1 == l - 1): max1 -= 1 if (itemLens[l - 1] == 0): itemLens.rotate() ind1 += 1 ind1 = ind1 % l if (ind1 == 0): ind1 = 1 max1 = itemLens[ind1] if (not state % n in table): if (n <= 1000): table[state % n] = adjust(max(itemLens), k) else: table[state % n] = adjust(max(max1, max2, itemLens[0]), k) #answer queries state = 0 for quer in queryString: if quer == '!': state = (state + 1) % n elif quer == '?': print(adjust(table[state], k))
def solution(new_id): answer = deq(new_id) result = "" while answer: cur = answer.popleft() if result == "" and cur == ".": continue if cur.isalpha(): result += cur.lower() elif cur.isdigit(): result += cur elif cur == "." and result[-1] == ".": continue elif cur == "-" or cur == "." or cur == "_": result += cur if result == "": return "aaa" if len(result) >= 16: result = result[:15] while True: if result[-1] == ".": result = result[:-1] else: break if len(result) <= 2: while len(result) < 3: result += result[-1] return result return result
def pre_order_traversal(node): cache = deq() cache.append(node) while len(cache) > 0: node = cache.popleft() if node._left is not None: cache.append(node._left) if node._right is not None: cache.append(node._right) yield node._value
def findMedianSortedArrays(nums1, nums2): n1 = n3 = deq(nums1) # n1, n2 as nums1, nums2 converted to deque n2 = deq(nums2) # n3 as the combination of n1 and n2 n3.extend(n2) n3 = deq(sorted(n3)) while len(n3) > 2: # pop left and right until 1 or 2 is left bot = n3.popleft() if n1[0] == bot: n1.popleft() else: n2.popleft() top = n3.pop() if len(n1) > 0 and n1[-1] == top: n1.pop() elif len(n2) > 0 and n2[-1] == top: n2.pop() ans = 0 for i in n3: ans += i return float(ans) / len(n3)
def _binfill(size: int): def b2q(n): return qbit_0 if n == 0b0 else qbit_1 states = list() up_lim = 2**size for x in range(0, up_lim): print(f"{x} is {x:b} in binary") arg = deq() n = x for f in range(size): #parsing each number into an array of bits arg.appendleft(b2q( n % 2)) #thus converting every bit into a basic qbit n = n >> 1 print(arg) q = qregister(*arg) states.append(q) return states
def dp_numways_opti(N, XmaybeNotSorted): from collections import deque as deq X = sorted(XmaybeNotSorted) # O(XlogX) dp = deq([0 for _ in range(X[-1] + 1)]) # O(X) dp[0] = 1 # dp is now a double-ended queue and we can use it as a circular buffer for i in range(0, N): # O(N*X) # print(i, ":", dp) for x in X: if i + x <= N: dp[x] += dp[ 0] # we replace i with 0 because we right-shift the buffer in every iteration # right-shift of the buffer: dp.popleft() if i + X[-1] < N: dp.append(0) # print(N, ":", dp) return dp[0]
def main(): n, q, k = [int(x) for x in input().strip().split()] oriarr = [int(x) for x in input().strip().split()] items = deq(oriarr) queryString = list(input()) state = 0 #there can be upto n-1 states including 0 table = {} #look-up table // python dictionary maxUpdates = min(n, queryString.count('!')) sp, ep, sp2, ep2 = seqcount(oriarr) #start and end pointers #cleaning the array for second max temparr = oriarr[:] pointer = sp turns = ep - sp + 1 while (turns > 0): turns -= 1 temparr.pop(pointer) #cleaning done temp1, temp2, temp3, temp4 = seqcount(temparr) sp2, ep2 = temp1, temp2 #adjusting sp2,ep2 if (sp2 >= sp): sp2 = sp2 + (ep - sp + 1) ep2 = ep2 + (ep - sp + 1) #print(sp,ep,sp2,ep2) table[0] = ep - sp + 1 #preparing the lookup table if (oriarr.count(1) == 0): for i in range(n): table[i] = 0 elif (oriarr.count(1) == n): for i in range(n): table[i] = n else: while (maxUpdates > 0): maxUpdates -= 1 state += 1 items.rotate(1) arr = list(items) sp = (sp + 1) % n sp2 = (sp2 + 1) % n ep = (ep + 1) % n ep2 = (ep2 + 1) % n if (sp <= ep and ep < n and arr[sp - 1] == 0): #print("YO1") #print(sp,ep,"HERE") #just shifting of sequence table[state] = ep - sp + 1 #now check if there is a larger sequence in front fleng = 0 for y in range(0, table[state]): if (arr[y] == 0): break else: fleng += 1 if (fleng >= table[state]): sp = 0 ep = fleng - 1 elif (sp <= ep and ep < n and arr[sp - 1] == 1): #print("YO2") #new element gets added in front table[state] = table[state - 1] + 1 sp = (sp - 1) % n elif (ep < sp): #sequence broken if (ep == 0): #print("YO3") #print(sp,ep,"HERE") if (arr[ep + 1] == 1): #chance that sequence may elongate for y in range(ep + 1, sp): if (arr[y] == 0): break else: ep += 1 oldlength = (n - 1) - sp + 1 newlength = ep + 1 if (oldlength > newlength): table[state] = table[state - 1] - 1 else: table[state] = newlength sp = 0 else: table[state] = table[state - 1] - 1 curr = table[state] tmp1, tmp2 = sp2, ep2 curr2 = tmp2 - tmp1 + 1 if (curr2 > curr): sp, ep = tmp1, tmp2 table[state] = curr2 elif (sp == ceil(n / 2) + 1): #print(sp,ep,"HERE") #half point is reached if (ep + 1 >= n - sp): #print("YO4") sp = 0 table[state] = ep - sp + 1 else: table[state] = table[state - 1] - 1 else: #print("YO5") table[state] = max(table[state - 1] - 1, ep + 1) curr = table[state] tmp1, tmp2 = sp2, ep2 curr2 = tmp2 - tmp1 + 1 if (curr2 > curr): sp, ep = tmp1, tmp2 table[state] = curr2 state = 0 for quer in queryString: if quer == '!': state = (state + 1) % n elif quer == '?': print(adjust(table[state], k))
from collections import deque as deq import sys sys.stdin=open('17822.circleBoard.txt') N,M,T = map(int,input().split()) board=[deq(int(x) for x in input().split()) for y in range(N)] for i in range(T): num, direc, kan = map(int,input().split()) for k in range(N): if (k+1)%num==0: if direc==0: # 시계방향 for j in range(kan): board[k].appendleft(board[k].pop()) else: # 반시계 for j in range(kan): board[k].append(board[k].popleft()) val=0 change=[] # 0이 될 좌표 for y in range(N): for x in range(M): if y<N-1: if x<M-1: if board[y][x+1]==board[y][x] and board[y][x+1]!=0: change.append((y,x + 1)) change.append((y,x)) val += 1 if board[y+1][x]==board[y][x] and board[y+1][x]!=0: change.append((y+1, x))
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from collections import deque as deq A = deq(list(input())) B = deq(list(input())) C = deq(list(input())) S = {'a': A, 'b': B, 'c': C} def check(s): if len(S[s]) == 0: return s.upper() else: return check(S[s].popleft()) print(check('a'))
def bfs(i, j, grid): deq = collections.deq() vi
def __init__(self): self.rate = rospy.Rate(100) self.flag1 = 1 self.flag2 = 1 self.h = 0 self.Va = 0 self.chi = 0 self.h_c = 0 self.Va_c = 0 self.chi_c = 0 self.delta_a = 0 self.delta_e = 0 self.delta_r = 0 self.delta_t = 0 self._delta_a = deq([]) self._delta_e = deq([]) self._delta_r = deq([]) self._delta_t = deq([]) self._h = deq([]) self._h_c = deq([]) self._chi_c = deq([]) self._chi = deq([]) self._Va = deq([]) self._Va_c = deq([]) self._t = deq([]) # subscribe to the MAV states and controller_commands rospy.Subscriber('/fixedwing/truth', State, self.get_states) rospy.Subscriber('/fixedwing/controller_commands', Controller_Commands, self.get_commands) rospy.Subscriber('/fixedwing/command', Command, self.get_deflections) # plotting variables self.app = pg.QtGui.QApplication([]) self.plotwin1 = pg.GraphicsWindow(size=(400,400)) self.plotwin2 = pg.GraphicsWindow(size=(400,400)) self.plotwin1.setWindowTitle('Commands vs. Actual') self.plotwin1.setInteractive(True) self.plotwin2.setWindowTitle('Control Commands') self.plotwin2.setInteractive(True) self.plt1 = self.plotwin1.addPlot(1,1) # plot for h self.plt2 = self.plotwin1.addPlot(2,1) # plot for chi self.plt3 = self.plotwin1.addPlot(3,1) # plot for Va self.plt4 = self.plotwin2.addPlot(1,1) # plot for delta_a self.plt5 = self.plotwin2.addPlot(2,1) # plot for delta_e self.plt6 = self.plotwin2.addPlot(4,1) # plot for delta_t # add axis labels self.plt1.setLabel('left', 'Height (m)') self.plt2.setLabel('left', 'Course Angle (deg)') self.plt3.setLabel('left', 'Velocity (m/s)') self.plt3.setLabel('bottom','Time (sec)') self.plt4.setLabel('left','Delta_a (rad)') self.plt5.setLabel('left','Delta_e (rad)') self.plt6.setLabel('left','Delta_t (%)') self.plt6.setLabel('bottom','Time (sec)') self.plt_h = self.plt1.plot(pen=pg.mkPen('r', width=2, style=pg.QtCore.Qt.SolidLine)) self.plt_h_c = self.plt1.plot(pen=pg.mkPen('b', width=2, style=pg.QtCore.Qt.DashLine)) self.plt_chi = self.plt2.plot(pen=pg.mkPen('r', width=2, style=pg.QtCore.Qt.SolidLine)) self.plt_chi_c = self.plt2.plot(pen=pg.mkPen('b', width=2, style=pg.QtCore.Qt.DashLine)) self.plt_Va = self.plt3.plot(pen=pg.mkPen('r', width=2, style=pg.QtCore.Qt.SolidLine)) self.plt_Va_c = self.plt3.plot(pen=pg.mkPen('b', width=2, style=pg.QtCore.Qt.DashLine)) self.plt_delta_a = self.plt4.plot(pen=pg.mkPen('g', width=2, style=pg.QtCore.Qt.SolidLine)) self.plt_delta_e = self.plt5.plot(pen=pg.mkPen('g', width=2, style=pg.QtCore.Qt.SolidLine)) self.plt_delta_t = self.plt6.plot(pen=pg.mkPen('g', width=2, style=pg.QtCore.Qt.SolidLine)) check=1 while self.flag1: if check: print 'waiting for states...' check=0 print 'states received...' check=1 while self.flag2: if check: print 'waiting for commands...' check=0 print 'commands received...'
val[num - 1] = (-val[num]) num -= 1 else: break else: break for i in range(len(val)): if val[i] == 1: deq[i].appendleft(deq[i].pop()) elif val[i] == (-1): deq[i].append(deq[i].popleft()) T = int(input()) for i in range(T): K = int(input()) sheet = [deq(int(x) for x in input().split()) for y in range(4)] spin = [[int(x) for x in input().split()] for y in range(K)] ans = 0 for j in range(K): check = [] num, dir = spin[j][0] - 1, spin[j][1] # 1이면 시계방향, -1이면 반시계 checker(num, sheet, dir) for k in range(4): ans += sheet[k][0] << k print('#{} {}'.format(i + 1, ans))