-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Kubysheva polina #135
base: Kubysheva_Polina
Are you sure you want to change the base?
Kubysheva polina #135
Changes from all commits
b37fbcd
b4e4245
3b88052
35f7609
9ff6778
a26aaf5
c58edc2
ee4ecde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
def main(): | ||
m = int(input()) | ||
c = list(map(int,input().split()))[:m] | ||
merge(c,0,len(c)) | ||
print(*c,sep=" ") | ||
def merge(c,start,end): | ||
if end - start > 1: | ||
middle = (start + end) // 2 | ||
merge(c,start,middle) | ||
merge(c,middle,end) | ||
left = c[start:middle] | ||
right = c[middle:end] | ||
inter_sort(c,left,right,start) | ||
print(start + 1, end, c[start], c[end-1]) | ||
def inter_sort(ar,left,right,start): | ||
j = i = 0 | ||
k = start | ||
while i < len(right) and j < len(left): | ||
if left[j] > right[i]: | ||
ar[k] = right[i] | ||
i+=1 | ||
else: | ||
ar[k] = left[j] | ||
j+=1 | ||
k+=1 | ||
while j < len(left): | ||
ar[k] = left[j] | ||
j+=1 | ||
k+=1 | ||
while i < len(right): | ||
ar[k] = right[i] | ||
i+=1 | ||
k+=1 | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
def merge(A, B): | ||
res = [] | ||
i = 0 | ||
j = 0 | ||
inver = 0 | ||
while i < len(A) and j < len(B): | ||
if A[i] <= B[j]: | ||
res.append(A[i]) | ||
i += 1 | ||
else: | ||
res.append(B[j]) | ||
j += 1 | ||
inver += len(A) - i | ||
while i < len(A): | ||
res.append(A[i]) | ||
i += 1 | ||
while j < len(B): | ||
res.append(B[j]) | ||
j += 1 | ||
return res, inver | ||
|
||
def MergeSort(L): | ||
n = len(L) | ||
if n <= 1: | ||
return L, 0 | ||
middle = n//2 | ||
A, l_inver = MergeSort(L[0:middle]) | ||
B, r_inver = MergeSort(L[middle:n]) | ||
res, inver = merge(A, B) | ||
return res, inver + l_inver + r_inver | ||
|
||
N = int(input()) | ||
L = list(map(int, input().split(" "))) | ||
L, itog_inver = MergeSort(L) | ||
print(itog_inver) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from math import gcd | ||
|
||
|
||
def build(v, l, r, it, a): #задаем переменные в функции | ||
if r-l == 1: # если выдает 1, то возвращаем | ||
it[v] = a[l] | ||
return | ||
m = (l+r)//2 # поиск m | ||
build(2*v + 1, l, m, it, a) | ||
build(2*v + 2, m, r, it, a) | ||
it[v] = gcd(it[2*v+1], it[2*v+2]) | ||
|
||
|
||
def getNod(v, l, r, it, ql, qr): #задаем переменные в функции | ||
if ql <= l and qr >= r: # если | ||
return it[v] | ||
if ql >= r or qr <= l: # если слева больше, чем справа или наоборот, то возвращаем 0 | ||
return 0 | ||
m = (l+r)//2 # поиск m | ||
tl = getNod(2*v + 1, l, m, it, ql, qr) # в tl получаем НОД | ||
tr = getNod(2*v + 2, m, r, it, ql, qr) # в tr получаем НОД | ||
return gcd(tl, tr) # выводим НОД tl and tr | ||
|
||
|
||
def upDate(v, l, r, it, indx, val): #задаем переменные в функции | ||
if r - l == 1: # если выдает 1, то возвращаем | ||
it[v] = val | ||
return | ||
middle = (r+l)//2 # находим середину | ||
if indx < middle: # если индекс меньше середины, то возвращается меньшее | ||
upDate(v*2+1, l, middle, it, indx, val) | ||
else: # если индекс большее середины, то возвращается большее | ||
upDate(v*2+2, middle, r, it, indx, val) | ||
it[v] = gcd(it[2*v+1], it[2*v+2]) # находим НОД | ||
|
||
|
||
def main(): #создаем функции | ||
n = int(input()) #ввод по условию | ||
it = [0]*(4*n) #ввод по условию | ||
a = list(map(int, input().split()))[:n] #создаеи список | ||
build(0, 0, n, it, a) | ||
q = int(input()) | ||
res = [] | ||
while q != 0: #пока не равно нулю выполняем: | ||
type_q, l, r = map(str, input().split()) | ||
if type_q == "s": # если это элементы левой или правой границы | ||
res.append(getNod(0, 0, n, it, int(l)-1, int(r))) #номер элемента и новое значение | ||
else: | ||
upDate(0, 0, n, it, int(l)-1, int(r)) | ||
q -= 1 | ||
print(*res) #выводим результат | ||
|
||
|
||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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 height(tree): | ||
if tree is None: | ||
return 0 | ||
return max(height(tree.left), height(tree.right))+1 | ||
|
||
def build_tree(elements): | ||
root = Node(elements[0]) | ||
for i in range(1, len(elements)): | ||
root.add(elements[i]) | ||
return root | ||
|
||
def balance(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 balance(tree.right) and balance(tree.left)): #условие для проверки баланса дерева(проверяем существование дерева,правой и левой частей, | ||
#разницу между ними,проверяем может ли работать метод для его правой и левой частей | ||
return True | ||
return False | ||
|
||
|
||
numbers = [int(i) for i in input().split()] | ||
numbers.pop() | ||
tree = build_tree(numbers) | ||
if balance(tree): | ||
print("YES") | ||
else: | ||
print("NO") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from math import gcd #подключаем библиотеку для нахождения НОД | ||
|
||
def build(v, l, r, it, nums): | ||
if r-l == 1: | ||
it[v] = nums[l] | ||
return | ||
m = (r+l)//2 | ||
build(2*v+1, l, m, it, nums) | ||
build(2*v+2, m, r, it, nums) | ||
it[v] = gcd(it[2*v+1], it[2*v+2]) | ||
|
||
def get_NOD(v, l, r, it, ql, qr): | ||
if ql <= l and qr >= r: | ||
return it[v] | ||
if ql >= r or qr <= l: | ||
return 0 | ||
m = (r+l)//2 | ||
tl = get_NOD(2*v+1, l, m, it, ql, qr) | ||
tr = get_NOD(2*v+2, m, r, it, ql, qr) | ||
return gcd(tl, tr) | ||
|
||
def main(): | ||
n = int(input()) | ||
nums = list(map(int, input().split())) | ||
it = [0]*4*n #массив для дерева | ||
build(0, 0, n, it, nums) | ||
q = int(input()) | ||
index = [] | ||
while q != 0: | ||
l, r = map(int, input().split()) | ||
index.append(get_NOD(0, 0, n, it, l-1, r)) | ||
q -= 1 | ||
print(*index) | ||
|
||
main() | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# этот код с вами писали | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Куда не посмотришь, везде все писали со мной) |
||
def shift_up(index, heap): | ||
while heap[index] > heap[(index-1)//2] and (index-1)//2 >= 0: | ||
heap[index], heap[(index-1)//2] = heap[(index-1)//2], heap[index] | ||
index = (index-1)//2 | ||
return index+1 | ||
|
||
|
||
|
||
def shift_down(index, heap): | ||
while 2*index+1 < len(heap): | ||
left_index = 2*index+1 | ||
right_index = 2*index+2 | ||
child_index = left_index | ||
if right_index < len(heap) and heap[left_index] < heap[right_index]: | ||
child_index = right_index | ||
if heap[child_index] <= heap[index]: | ||
break | ||
heap[index], heap[child_index] = heap[child_index], heap[index] | ||
index = child_index | ||
return index+1 | ||
|
||
|
||
def add(item, heap): # добавление | ||
heap.append(item) | ||
return shift_up(len(heap)-1, heap) | ||
|
||
|
||
def extract(heap): # извлечение | ||
heap[0], heap[len(heap)-1] = heap[len(heap)-1], heap[0] | ||
ind = heap.pop() | ||
if heap: | ||
return shift_down(0, heap), ind | ||
else: | ||
return 0, ind | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это тоже со мной писали? |
||
|
||
|
||
def get_max(heap): | ||
return heap[0] | ||
|
||
|
||
def main(): | ||
heap = [] # создаем пустой массив | ||
result = [] #создаём пустой массив | ||
input_ = input().split() # ввод в консоли | ||
n, m = int(input_[0]), int(input_[1]) | ||
for i in range(m): # генерируем числа | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Какие же мы числа генерируем? |
||
data = input().split() # ввод в консоли | ||
if int(data[0]) == 1: | ||
if not heap: | ||
result.append(-1) # если нельзя добавить элемент,добавляем в конец списка -1 | ||
else: # иначе вставляем в конец списка элемент,который извлекли | ||
result.append(extract(heap)) | ||
elif int(data[0]) == 2: | ||
if len(heap) == n: | ||
result.append(-1) | ||
else: | ||
result.append(add(int(data[-1]), heap)) | ||
for i in result: | ||
if type(i) == tuple: | ||
print(*i) | ||
else: | ||
print(i) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. И это писали?) код с 54-63 |
||
print(*heap) | ||
|
||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# писали с вами ,комментарии естть к тому,что дописать надо было | ||
def shift_down(value, heap): | ||
while 2*value+1 < len(heap): | ||
left_ind = 2*value+1 | ||
right_ind = 2*value+2 | ||
ind = 2*value + 1 | ||
if right_ind < len(heap) and heap[left_ind] < heap[right_ind]: | ||
ind = right_ind | ||
if heap[ind] <= heap[value]: | ||
break | ||
heap[value], heap[ind] = heap[ind], heap[value] | ||
value = ind | ||
|
||
|
||
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()) #вводим длину массива | ||
arr = [] | ||
numbers = list(map(int, input().split()))[:n] | ||
heap = build(numbers) | ||
for i in range(n): | ||
print(*heap) | ||
arr.append(extract(heap)) #добавляем число в конец из экстракта | ||
arr.reverse() | ||
print(*arr) #выводим на консоль | ||
|
||
|
||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
def shift_up(c, heap_): | ||
while heap_[c] > heap_[(c-1)//2] and (c-1)//2 >= 0: | ||
heap_[c], heap_[(c-1)//2] = heap_[(c-1)//2], heap_[c] | ||
c = (c-1)//2 | ||
return c+1 | ||
|
||
def shift_down(c, heap_): | ||
while 2*c+1 < len(heap_): | ||
left = 2*c+1 | ||
right = 2*c+2 | ||
child = left | ||
if right < len(heap_) and heap_[left] < heap_[right]: | ||
child = right | ||
if heap_[child] <= heap_[c]: | ||
break | ||
heap_[c], heap_[child] = heap_[child], heap_[c] | ||
c = child | ||
return c+1 | ||
|
||
def add(item, heap_): | ||
heap_.append(item) | ||
return shift_up(len(heap_)-1, heap_) | ||
|
||
def extract(heap_): | ||
if len(heap)==1: | ||
x = heap_.pop() | ||
return [0, x] | ||
else: | ||
heap_[0], heap_[len(heap_)-1] = heap_[len(heap_)-1], heap_[0] | ||
x = heap_.pop() | ||
y = shift_down(0, heap_) | ||
return [y, x] | ||
|
||
def extract_2_o(item, heap_): | ||
v = heap[len(heap)-1] | ||
heap_[item], heap_[len(heap_)-1] = heap_[len(heap_)-1], heap_[item] | ||
index_ = heap.pop() | ||
if v > index_: | ||
shift_up(item, heap_) | ||
else: | ||
shift_down(item, heap_) | ||
return index_ | ||
|
||
|
||
n = list(map(int,input().split())) | ||
final_heap = [] | ||
heap = [] | ||
for i in range(n[1]): | ||
op = list(map(int, input().split())) | ||
if op[0] == 1: | ||
if heap: | ||
final_heap.append(extract(heap)) | ||
else: | ||
final_heap.append(-1) | ||
elif op[0] == 2: | ||
if len(heap) < n[0]: | ||
final_heap.append(add(op[1], heap)) | ||
else: | ||
final_heap.append(-1) | ||
else: | ||
if len(heap) >= op[1] and op[1] > 0: | ||
final_heap.append(extract_2_o(op[1]-1, heap)) | ||
else: | ||
final_heap.append(-1) | ||
|
||
for i in final_heap: | ||
if type(i) == list: | ||
print(*i) | ||
else: | ||
print(i) | ||
print(*heap) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. И тут все со мной писали... какая я молодец) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
просто огонь, а не комментарий)