diff --git a/.vscode/settings.json b/.vscode/settings.json index d10cb1f..16b224d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,4 +2,5 @@ "python.linting.pylintEnabled": false, "python.linting.mypyEnabled": true, "python.linting.enabled": true, + "python.pythonPath": "C:\\Users\\dmitr\\AppData\\Local\\Programs\\Python\\Python39\\python.exe", } \ No newline at end of file diff --git a/src/main.py b/src/main.py index 6d95fe9..5e6a6f8 100644 --- a/src/main.py +++ b/src/main.py @@ -1 +1,2 @@ -print("Hello world") \ No newline at end of file +a,b=(int(s) for s in input().split()) +print(a+b) \ No newline at end of file diff --git a/src/module2_1.py b/src/module2_1.py new file mode 100644 index 0000000..2687725 --- /dev/null +++ b/src/module2_1.py @@ -0,0 +1,19 @@ +n = int(input()) +arr = input() +lst = arr.split(' ') +res = [] +s = True +for item in lst: + res.append(int(item)) +n = len(res) +for i in range(0,n-1): + for j in range(0,n-1-i): + if res[j] > res[j+1]: + s = False + temp = res[j+1] + res[j+1] = res[j] + res[j] = temp + print(" ".join(map(str, res))) + +if s == True: + print(0) diff --git a/src/module2_2.py b/src/module2_2.py new file mode 100644 index 0000000..960bbea --- /dev/null +++ b/src/module2_2.py @@ -0,0 +1,18 @@ +def sort_pair(A, n): + for i in range(1, n): + for j in range(0, n-i): + if A[j][1] < A[j+1][1]: + A[j],A[j+1] = A[j+1],A[j] + elif A[j][1] == A[j+1][1] and A[j][0] > A[j+1][0]: + A[j],A[j+1] = A[j+1],A[j] + for i in range(0, n): + print(" ".join(map(str,A[i]))) + +D = int(input()) +A = [0]*D +for i in range(0, D): + A[i] = input() + A[i] = A[i].split(" ") + A[i] = list(map(int,A[i])) + +sort_pair(A, D) \ No newline at end of file diff --git a/src/module2_3.py b/src/module2_3.py new file mode 100644 index 0000000..74f0243 --- /dev/null +++ b/src/module2_3.py @@ -0,0 +1,37 @@ +def merge(Arr, List): + new_list = [] + d, m = 0, 0 + while d < len(Arr) and m < len(List): + if Arr[d] < List[m]: + new_list.append(Arr[d]) + d += 1 + else: + new_list.append(List[m]) + m += 1 + while d < len(Arr): + new_list.append(Arr[d]) + d += 1 + while m < len(List): + new_list.append(List[m]) + m += 1 + + return new_list + +def mergeSort(Arr, index): + if len(Arr) < 2: + return Arr + else: + mid = len(Arr)//2 + left = mergeSort(Arr[:mid], [index[0],index[0]+mid]) + right = mergeSort(Arr[mid:],[index[0]+mid, index[1]]) + + sort = merge(left, right) + print(index[0]+1, index[1], sort[0], sort[-1]) + return sort + + +Quantity = int(input()) +Arr = list(map(int, input().split())) + +Arr = mergeSort(Arr,[0, Quantity]) +print(' '.join(map(str, Arr))) \ No newline at end of file diff --git a/src/module2_4.py b/src/module2_4.py new file mode 100644 index 0000000..143defd --- /dev/null +++ b/src/module2_4.py @@ -0,0 +1,45 @@ +def Sort(Arr, Num): + List = [0]*Num + return merge_sort(Arr, List, 0, Num-1) + +def merge_sort(Arr, List, left, right): + Meter = 0 + if left < right: + mid = (left + right)//2 + Meter += merge_sort(Arr, List, left, mid) + Meter += merge_sort(Arr, List, mid + 1, right) + Meter += Merge(Arr, List, left, mid, right) + return Meter + +def Merge(Arr, List, left, mid, right): + i = left + j = mid + 1 + u = left + Meter = 0 + while i <= mid and j <= right: + if Arr[i] <= Arr[j]: + List[u] = Arr[i] + u += 1 + i += 1 + else: + List[u] = Arr[j] + Meter += (mid-i + 1) + u += 1 + j += 1 + while i <= mid: + List[u] = Arr[i] + u += 1 + i += 1 + while j <= right: + List[u] = Arr[j] + u += 1 + j += 1 + for now in range(left, right + 1): + Arr[now] = List[now] + return Meter + +Quantity = int(input()) +Arr = list(map(int, input().split())) +Num = len(Arr) +Arr = Sort(Arr, Num) +print(Arr) \ No newline at end of file diff --git a/src/module2_5.py b/src/module2_5.py new file mode 100644 index 0000000..d7531e7 --- /dev/null +++ b/src/module2_5.py @@ -0,0 +1,8 @@ +d = 1 +m = int(input()) +numbers = list(map(int, input().split(maxsplit = m))) +numbers.sort() +for c in range(1, m): + if numbers[c-1] != numbers[c]: + d= d+1 +print(d) \ No newline at end of file diff --git a/src/module2_6.py b/src/module2_6.py new file mode 100644 index 0000000..9cd7595 --- /dev/null +++ b/src/module2_6.py @@ -0,0 +1,15 @@ +quantity_product = int(input()) +product = list(map(int, input().split())) + +quantily_purpose = int(input()) +purpose = list(map(int,input().split())) + +calculate = [0] * (quantity_product+1) +for now in purpose: + calculate[now] += 1 + +for i in range(quantity_product): + if product[i] < calculate[i+1]: + print("yes") + else: + print("no") \ No newline at end of file diff --git a/src/module2_7.py b/src/module2_7.py new file mode 100644 index 0000000..ab37bc6 --- /dev/null +++ b/src/module2_7.py @@ -0,0 +1,26 @@ +def store(D, k): + digit = len(D[0]) + for c in range(digit): + mass = [[] for f in range(10)] + for item in D: + index = int(item)//10**c % 10 + mass[index].append(item) + print("Phase", c+1) + D = [] + for c in range(10): + print("Bucket ", c ,":", sep = "", end=" ") + print(", ".join(mass[c]) if len(mass[c])>0 else "empty") + D += mass[c] + print("**********") + return D + +k = int(input()) +D = [] +for i in range(k): + D.append(input()) +print("Initial array:") +print(", ".join(D)) +print("**********") +D = store(D, k) +print("Sorted array:") +print(", ".join(D)) \ No newline at end of file diff --git a/src/module3_1.py b/src/module3_1.py new file mode 100644 index 0000000..738bfd4 --- /dev/null +++ b/src/module3_1.py @@ -0,0 +1,30 @@ +def hash(A, x): + hash_str = 0 + for i in range(x): + hash_str = (hash_str * p + ord(A[i])) % q + return hash_str + +def rabin_karp(str, sub_str, x, m): + lst = [] + str_hash = hash(str, m) + sub_str_hash = hash(sub_str, m) + pt = 1 + for i in range(m-1): + pt = (pt * p) % q + for i in range(1, x-m+2): + if str_hash == sub_str_hash: + lst.append(i-1) + if i < x-m+1: + str_hash = (str_hash - ord(str[i-1]) * pt) * p + str_hash += ord(str[i + m - 1]) + str_hash %= q + return lst + +string = input() +sub_string = input() +global p +p = 31 +global q +q = 2 ** 31 - 1 +A = rabin_karp(string, sub_string, len(string), len(sub_string)) +print(' '.join(map(str, A))) \ No newline at end of file diff --git a/src/module3_2.py b/src/module3_2.py new file mode 100644 index 0000000..1315c91 --- /dev/null +++ b/src/module3_2.py @@ -0,0 +1,40 @@ +def shift(string_two, string_one, q): + + if string_one == string_two: + return 0 + + string_one += string_one + + d = 11 + m = 1 + s1hash = 0 + for i in string_two[::-1]: + s1hash += ord(i) * m + m *= d + s1hash %= q + m %= q + + m = 1 + hash = 0 + for i in string_one[:len(string_two)][::-1]: + hash += m * ord(i) + m *= d + hash %= q + m %= q + + hashtag = 1 + for i in range(len(string_two) - 1): + hashtag *= d + hashtag %= q + for i in range(1, len(string_one) - len(string_two) + 1): + hashnew = ((hash % q - ord(string_one[i - 1]) * hashtag % q) * d % q + ord(string_one[i + len(string_two) - 1])) % q + if hashnew == s1hash: + return i + hash = hashnew + + return -1 + +string_one = input() +string_two = input() +q = 2 ** 31 - 1 +print(shift(string_one, string_two, q)) \ No newline at end of file diff --git a/src/module3_3.py b/src/module3_3.py new file mode 100644 index 0000000..fdb08e1 --- /dev/null +++ b/src/module3_3.py @@ -0,0 +1,27 @@ +def pref_func(s): + length = len(s) + Meter = [0] * length + Meter[0] = 0 + for i in range (length - 1): + j = Meter[i] + while (j > 0) and (s[i + 1] != s[j]): + j = Meter[j - 1] + if (s[i + 1] == s[j]): + Meter[i+1] = j + 1 + else: + Meter[i + 1] = 0 + return Meter + +text = str(input()) +s = list(text) + +length = len(s) +pref = pref_func(s) + +for i in range(length - 1, length): + result = length - pref[i] + +if (length % result == 0): + print(length // result) +else: + print('1') \ No newline at end of file diff --git a/src/module3_4.py b/src/module3_4.py new file mode 100644 index 0000000..19b1cbc --- /dev/null +++ b/src/module3_4.py @@ -0,0 +1,19 @@ +def pref_func(s): + length = len(s) + Meter = [0] * length + Meter[0] = 0 + for i in range (length - 1): + j = Meter[i] + while (j > 0) and (s[i + 1] != s[j]): + j = Meter[j - 1] + if (s[i + 1] == s[j]): + Meter[i+1] = j + 1 + else: + Meter[i + 1] = 0 + return Meter +text = str(input()) +s = list(text) + +length = len(s) +pref = pref_func(s) +print(length - pref[-1]) \ No newline at end of file diff --git a/src/module4_1.py b/src/module4_1.py new file mode 100644 index 0000000..62d697d --- /dev/null +++ b/src/module4_1.py @@ -0,0 +1,14 @@ +def PSP(s): + Meter = 0 + new_list = [] + for i in s: + if (i == '('): + new_list.append(i) + elif (new_list != []) and (new_list[-1] == '('): + new_list.pop() + else: + Meter += 1 + return Meter+len(new_list) + +s = str(input()) +print(PSP(s)) \ No newline at end of file diff --git a/src/module4_2.py b/src/module4_2.py new file mode 100644 index 0000000..8edf0bd --- /dev/null +++ b/src/module4_2.py @@ -0,0 +1,24 @@ +length = int(input()) +traject_a = list(map(int,input().split())) +traject_b = [] +stack = [] +test = sorted(traject_a) +for i in range(length): + if (stack == []) or (stack[-1] > traject_a[0]): + stack.append(traject_a[0]) + traject_a.pop(0) + else: + while (stack != [] and stack[-1] < traject_a[0]): + traject_b.append(stack[-1]) + stack.pop(-1) + if (stack == []) or (stack[-1] > traject_a[0]): + stack.append(traject_a[0]) + traject_a.pop(0) +if stack != []: + for i in range(len(stack)): + traject_b.append(stack[-1]) + stack.pop(-1) +if traject_b == test: + print("YES") +else: + print("NO") \ No newline at end of file diff --git a/src/module4_3.py b/src/module4_3.py new file mode 100644 index 0000000..cffa619 --- /dev/null +++ b/src/module4_3.py @@ -0,0 +1,13 @@ +length = int(input()) +x = list(zip(list(map(int, input().split())), range(length))) +stack = [] +result = [0]*length +for i in reversed(x): + while (stack != []) and (stack[-1][0] >= i[0]): + stack.pop() + if stack == []: + result[i[1]] = -1 + else: + result[i[1]] = stack[-1][1] + stack.append(i) +print(' '.join(map(str, result))) \ No newline at end of file diff --git a/src/module4_4.py b/src/module4_4.py new file mode 100644 index 0000000..4d82f0b --- /dev/null +++ b/src/module4_4.py @@ -0,0 +1,26 @@ +from collections import deque + +def scroll_wind(a, lengt, len_wind): + deq = deque() + + for i in range(len_wind): + while (deq) and (a[i] < a[deq[-1]]) : + deq.pop() + deq.append(i) + + for i in range(len_wind, lengt): + print(a[deq[0]]) + + while (deq) and (deq[0] <= i-len_wind): + deq.popleft() + + while (deq) and (a[i] < a[deq[-1]]) : + deq.pop() + deq.append(i) + + print(a[deq[0]]) + +lengt, len_wind = (int(i) for i in input().split()) +a = list(map(int,input().split())) + +scroll_wind(a, lengt, len_wind) \ No newline at end of file diff --git a/src/module5_1.py b/src/module5_1.py new file mode 100644 index 0000000..14452c4 --- /dev/null +++ b/src/module5_1.py @@ -0,0 +1,43 @@ +class treeNode: + _size = 0 + def __init__(self, data): + self.data = data + self.left = None + self.right = None + treeNode._size += 1 + + def add(self, amount): + if amount == self.data: + return + if amount < self.data: + if self.left: + self.left.add(amount) + else: + self.left = treeNode(amount) + else: + if self.right: + self.right.add(amount) + else: + self.right = treeNode(amount) + + def findFork(self): + if self.left: + self.left.findFork() + if (self.left and self.right is None) or (self.left is None and self.right): + print(self.data) + if self.right: + self.right.findFork() + +def buildTree(elements): + result = treeNode(elements[0]) + for i in range(1, len(elements)): + result.add(elements[i]) + return result + +def main(): + numbers = list(map(int, input().split(" "))) + numbers.pop() + tree = buildTree(numbers) + tree.findFork() + +main() \ No newline at end of file diff --git a/src/module5_2.py b/src/module5_2.py new file mode 100644 index 0000000..43db8d4 --- /dev/null +++ b/src/module5_2.py @@ -0,0 +1,46 @@ +class Node: + def __init__(self, data): + self.data = data + self.left = None + self.right = None + def add(self, data): + if data == self.data: + return + if data < self.data: + if self.left: + self.left.add(data) + else: + self.left = Node(data) + else: + if self.right: + self.right.add(data) + else: + self.right = Node(data) + +def balan(tree): + if not tree or ((height(tree.left) == height(tree.right) or height(tree.left)+1 == height(tree.right) or height(tree.left) == height(tree.right)+1) and balan(tree.right) and balan(tree.left)): + return True + return False + +def height(tree): + if tree is None: + return 0 + return max(height(tree.left), height(tree.right))+1 + +def build_tree(elm): + root = Node(elm[0]) + for i in range(1, len(elm)): + root.add(elm[i]) + return root + + +def main(): + num = [int(i) for i in input().split()] + num.pop() + tree = build_tree(num) + if balan(tree): + print("YES") + else: + print("NO") + +main() \ No newline at end of file diff --git a/src/module5_3.py b/src/module5_3.py new file mode 100644 index 0000000..acaa2e8 --- /dev/null +++ b/src/module5_3.py @@ -0,0 +1,35 @@ +from math import gcd + +def build(v, x, r, it, num): + if r-x == 1: + it[v] = num[x] + return + m = (r+x)//2 + build(2*v+1, x, m, it, num) + build(2*v+2, m, r, it, num) + it[v] = gcd(it[2*v+1], it[2*v+2]) + +def get_NOD(v, x, r, it, ql, qr): + if ql <= x and qr >= r: + return it[v] + if ql >= r or qr <= x: + return 0 + m = (r+x)//2 + tl = get_NOD(2*v+1, x, m, it, ql, qr) + tr = get_NOD(2*v+2, m, r, it, ql, qr) + return gcd(tl, tr) + +def main(): + n = int(input()) + num = list(map(int, input().split())) + it = [0]*4*n + build(0, 0, n, it, num) + q = int(input()) + index = [] + while q != 0: + x, r = map(int, input().split()) + index.append(get_NOD(0, 0, n, it, x-1, r)) + q -= 1 + print(*index) + +main() \ No newline at end of file diff --git a/src/module5_4.py b/src/module5_4.py new file mode 100644 index 0000000..f5ec884 --- /dev/null +++ b/src/module5_4.py @@ -0,0 +1,48 @@ +def building_tree(x, lef, righ, s_tree, a): + if righ-lef == 1: + s_tree[x] = a[lef] + return + m = (righ+lef)//2 + building_tree(2*x+1, lef, m, s_tree, a) + building_tree(2*x+2, m, righ, s_tree, a) + s_tree[x] = s_tree[2*x+1] + s_tree[2*x+2] + +def search(x, lef, righ, s_tree, k): + if k > s_tree[x]: + return -1 + if righ - lef == 1: + return righ + m = (righ+lef)//2 + if s_tree[2*x+1] >= k: + return search(2*x+1, lef, m, s_tree, k) + else: + return search(2*x+2, m, righ, s_tree, k - s_tree[2*x+1]) + +def get_sum(x, lef, righ, s_tree, ql, qr): + if ql <= lef and qr >= righ: + return s_tree[x] + if ql >= righ or qr <= lef: + return 0 + m = (righ+lef)//2 + s_left = get_sum(2*x+1, lef, m, s_tree, ql, qr) + s_right = get_sum(2*x+2, m, righ, s_tree, ql, qr) + return s_left + s_right +def main(): + num = int(input()) + a = [0 if int(i) != 0 else 1 for i in input().split()] + s_tree = [0]*4*num + building_tree(0, 0, num, s_tree, a) + q = int(input()) + index = [] + while q != 0: + lef, righ, k = map(int, input().split()) + sum = get_sum(0, 0, num, s_tree, lef-1, righ) + if sum >= k and lef > 1: + index.append(search(0, 0, num, s_tree, k+get_sum(0, 0, num, s_tree, 0, lef-1))) + elif sum >= k and lef == 1: + index.append(search(0, 0, num, s_tree, k)) + else: + index.append(-1) + q -= 1 + print(*index) +main() \ No newline at end of file diff --git a/src/module5_5.py b/src/module5_5.py new file mode 100644 index 0000000..fe359cb --- /dev/null +++ b/src/module5_5.py @@ -0,0 +1,50 @@ +from math import gcd + + +def build(x1, x2, x3, x4, x5): + if x3-x2 == 1: + x4[x1] = x5[x2] + return + d = (x2+x3)//2 + build(2*x1 + 1, x2, d, x4, x5) + build(2*x1 + 2, d, x3, x4, x5) + x4[x1] = gcd(x4[2*x1 + 1], x4[2*x1 + 2]) + +def update(x1, x2, x3, x4, index, value): + if x3 - x2 == 1: + x4[x1] = value + return + middle = (x3+x2)//2 + if index < middle: + update(x1*2+1, x2, middle, x4, index, value) + else: + update(x1*2+2, middle, x3, x4, index, value) + x4[x1] = gcd(x4[2*x1 + 1], x4[2*x1 + 2]) + +def get_nod(x1, x2, x3, x4, proz1, proz2): + if proz1 <= x2 and proz2 >= x3: + return x4[x1] + if proz1 >= x3 or proz2 <= x2: + return 0 + d = (x2+x3)//2 + tl = get_nod(2*x1 + 1, x2, d, x4, proz1, proz2) + tr = get_nod(2*x1 + 2, d, x3, x4, proz1, proz2) + return gcd(tl, tr) + +def main(): + n = int(input()) + x4 = [0]*(4*n) + x5 = list(map(int, input().split()))[:n] + build(0, 0, n, x4, x5) + q = int(input()) + res = [] + while q !=0: + type_q, x2, x3 = map(str, input().split()) + if type_q == "s": + res.append(get_nod(0, 0, n, x4, int(x2)-1, int(x3))) + else: + update(0, 0, n, x4, int(x2)-1, int(x3)) + q-=1 + print(*res) + +main() \ No newline at end of file diff --git a/src/module5_6.py b/src/module5_6.py new file mode 100644 index 0000000..97fc4e3 --- /dev/null +++ b/src/module5_6.py @@ -0,0 +1,67 @@ +def building_tree(x, lef, righ, s_tree, a): + if righ-lef == 1: + s_tree[x] = a[lef] + return + m = (righ+lef)//2 + building_tree(2*x+1, lef, m, s_tree, a) + building_tree(2*x+2, m, righ, s_tree, a) + s_tree[x] = s_tree[2*x+1] + s_tree[2*x+2] + +def search(x, lef, righ, s_tree, k): + if k > s_tree[x]: + return -1 + if righ - lef == 1: + return righ + m = (righ+lef)//2 + if s_tree[2*x+1] >= k: + return search(2*x+1, lef, m, s_tree, k) + else: + return search(2*x+2, m, righ, s_tree, k - s_tree[2*x+1]) + +def get_sum(x, lef, righ, s_tree, left, right): + if left <= lef and right >= righ: + return s_tree[x] + if left >= righ or right <= lef: + return 0 + m = (righ+lef)//2 + t_left = get_sum(2*x+1, lef, m, s_tree, left, right) + t_right = get_sum(2*x+2, m, righ, s_tree, left, right) + return t_left + t_right + +def up(x, lef, righ, s_tree, ind_new, value): + if righ-lef == 1: + s_tree[x] = value + return + m = (righ+lef)//2 + if ind_new < m: + up(2*x+1, lef, m, s_tree, ind_new, value) + else: + up(2*x+2, m, righ, s_tree, ind_new, value) + s_tree[x] = s_tree[2*x+1] + s_tree[2*x+2] +def main(): + n = int(input()) + a = [0 if int(i) != 0 else 1 for i in input().split()] + s_tree = [0]*4*n + building_tree(0, 0, n, s_tree, a) + q = int(input()) + index = [] + while q != 0: + i = input().split() + if len(i) == 4: + lef, righ, k = int(i[1]), int(i[2]), int(i[3]) + summ = get_sum(0, 0, n, s_tree, lef-1, righ) + if summ >= k and lef > 1: + index.append(search(0, 0, n, s_tree, k+get_sum(0, 0, n, s_tree, 0, lef-1))) + elif summ >= k and lef == 1: + index.append(search(0, 0, n, s_tree, k)) + else: + index.append(-1) + else: + i, x = int(i[1]), int(i[2]) + if x == 0: + up(0, 0, n, s_tree, i-1, 1) + else: + up(0, 0, n, s_tree, i-1, 0) + q -= 1 + print(*index) +main() \ No newline at end of file diff --git a/src/module6_1.py b/src/module6_1.py new file mode 100644 index 0000000..3c7e674 --- /dev/null +++ b/src/module6_1.py @@ -0,0 +1,56 @@ +def shift_up(x, heap): + while heap[x] > heap[(x-1)//2] and (x-1)//2 >= 0: + heap[x], heap[(x-1)//2] = heap[(x-1)//2], heap[x] + x = (x-1)//2 + return x+1 + +def shift_down(x, heap): + while 2*x+1 < len(heap): + left_index = 2*x+1 #идекса левого элемента + right_index = 2*x+2 #индекса правого элемента + index = 2*x+1 + if right_index < len(heap) and heap[left_index] < heap[right_index]: + index = right_index + if heap[index] <= heap[x]: + break + heap[x], heap[index] = heap[index], heap[x] + x = index + return x+1 + +def add(amount, heap): #добавление вершины + heap.append(amount) + return shift_up(len(heap)-1, heap) + +def extract(heap): + heap[0], heap[len(heap)-1] = heap[len(heap)-1], heap[0] + x = heap.pop() + if heap: + return shift_down(0, heap), x + else: + return 0, x + +def main(): + heap = [] + res = [] + amount = input().split() + n, m = int(amount[0]), int(amount[1]) #максимальный размер приоритетной очереди и количество запросов + for amount in range(m): + data = input().split() + if int(data[0]) == 1: + if not heap: + res.append(-1) + else: + res.append(extract(heap)) + elif int(data[0]) == 2: + if len(heap) == n: + res.append(-1) + else: + res.append(add(int(data[-1]), heap)) + for i in res: + if type(i) == tuple: + print(*i) + else: + print(i) + print(*heap) + +main() \ No newline at end of file diff --git a/src/module6_2.py b/src/module6_2.py new file mode 100644 index 0000000..927246a --- /dev/null +++ b/src/module6_2.py @@ -0,0 +1,71 @@ +def shift_up(x, heap): + while heap[x] > heap[(x-1)//2] and (x-1)//2 >= 0: + heap[x], heap[(x-1)//2] = heap[(x-1)//2], heap[x] + x = (x-1)//2 + return x+1 + +def shift_down(x, heap): + while 2*x+1 < len(heap): + left_index = 2*x+1 + right_index = 2*x+2 + index = left_index + if right_index < len(heap) and heap[left_index] < heap[right_index]: + index = right_index + if heap[index] <= heap[x]: + break + heap[x], heap[index] = heap[index], heap[x] + x = index + return x+1 + +def add(item, heap): + heap.append(item) + return shift_up(len(heap)-1, heap) + +def direct_extract(x, heap): + i = heap[len(heap)-1] + heap[x-1], heap[len(heap)-1] = heap[len(heap)-1], heap[x-1] + inda = heap.pop() + if i < inda: + shift_down(x-1, heap) + elif i > inda: + shift_up(x-1, heap) + return inda + +def extract(heap): + heap[0], heap[len(heap)-1] = heap[len(heap)-1], heap[0] + inda = heap.pop() + if heap: + return shift_down(0, heap), inda + else: + return 0, inda + +def main(): + heap = [] + res = [] + amount = input().split() + n, m = int(amount[0]), int(amount[1]) + for amount in range(m): + data = input().split() + if int(data[0]) == 1: + if not heap: + res.append(-1) + else: + res.append(extract(heap)) + elif int(data[0]) == 2: + if len(heap) == n: + res.append(-1) + else: + res.append(add(int(data[-1]), heap)) + elif int(data[0]) == 3: + if len(heap) >= int(data[-1]) and int(data[-1]) > 0: + res.append(direct_extract(int(data[-1]), heap)) #значение удалённого элемента + else: + res.append(-1) + for amount in res: + if type(amount) == tuple: + print(*amount) + else: + print(amount) + print(*heap) + +main() \ No newline at end of file diff --git a/src/module6_3.py b/src/module6_3.py new file mode 100644 index 0000000..7c1306e --- /dev/null +++ b/src/module6_3.py @@ -0,0 +1,39 @@ +def shift_down(x, heap): + while 2*x+1 < len(heap): + left_index = 2*x+1 + right_index = 2*x+2 + index = 2*x+1 + if right_index < len(heap) and heap[left_index] < heap[right_index]: + index = right_index + if heap[index] <= heap[x]: + break + heap[x], heap[index] = heap[index], heap[x] + x = index + +def extract(heap): + heap[0], heap[-1] = heap[-1], heap[0] + a = heap.pop() + shift_down(0, heap) + return a + +def get_max(heap): + return heap[0] + +def build(arr): #дерево + heap = arr[:] + for i in range(len(heap)-1, -1, -1): + shift_down(i, heap) + return heap + +def main(): + n = int(input()) + res = [] + numbers = list(map(int, input().split()))[:n] #вводим элементы массива + heap = build(numbers) #куча + for i in range(n): + print(*heap) + res.append(extract(heap)) + res.reverse() + print(*res) + +main() \ No newline at end of file