From d7dff9e37d2c6abe64279f6217baeabd86029a68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=83=D0=B3=D0=B0=D1=81=D0=BE=D0=B2=20=D0=A1=D1=82?= =?UTF-8?q?=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Fri, 12 Feb 2021 18:50:54 +0300 Subject: [PATCH 01/24] task a --- src/main.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index 6d95fe9..9d38638 100644 --- a/src/main.py +++ b/src/main.py @@ -1 +1,7 @@ -print("Hello world") \ No newline at end of file +inp = input() +splt = inp.split(" ") +print(splt) +res = [] +for item in splt: + res.append(int(item)) +print(res[0]+res[1]) \ No newline at end of file From 4f6814bb934ebf8f7b5249c26d3a506ab2c77555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=83=D0=B3=D0=B0=D1=81=D0=BE=D0=B2=20=D0=A1=D1=82?= =?UTF-8?q?=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Fri, 12 Feb 2021 19:06:05 +0300 Subject: [PATCH 02/24] ab --- src/main.py | 8 +------- src/not the main copy.py | 1 + 2 files changed, 2 insertions(+), 7 deletions(-) create mode 100644 src/not the main copy.py diff --git a/src/main.py b/src/main.py index 9d38638..1596831 100644 --- a/src/main.py +++ b/src/main.py @@ -1,7 +1 @@ -inp = input() -splt = inp.split(" ") -print(splt) -res = [] -for item in splt: - res.append(int(item)) -print(res[0]+res[1]) \ No newline at end of file +print("Hello, " + input() + "!") \ No newline at end of file diff --git a/src/not the main copy.py b/src/not the main copy.py new file mode 100644 index 0000000..1596831 --- /dev/null +++ b/src/not the main copy.py @@ -0,0 +1 @@ +print("Hello, " + input() + "!") \ No newline at end of file From 73a0d5aa483cca80b157148e5deb0267f441a152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=83=D0=B3=D0=B0=D1=81=D0=BE=D0=B2=20=D0=A1=D1=82?= =?UTF-8?q?=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Mon, 8 Mar 2021 23:25:03 +0300 Subject: [PATCH 03/24] task 2 --- src/insertion_sort.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/insertion_sort.py diff --git a/src/insertion_sort.py b/src/insertion_sort.py new file mode 100644 index 0000000..33703ec --- /dev/null +++ b/src/insertion_sort.py @@ -0,0 +1,16 @@ + +mas = [[int(i) for i in input().split(" ")]for _ in range(int(input()))] + + +for j in range( len(mas)): + for i in range( len(mas)-1): + if(mas[i][1] < mas[i+1][1]): + mas[i][0],mas[i+1][0] = mas[i+1][0],mas[i][0] + mas[i][1],mas[i+1][1] = mas[i+1][1],mas[i][1] + if(mas[i][1] == mas[i+1][1]): + if(mas[i][0] > mas[i+1][0]): + mas[i][0],mas[i+1][0] = mas[i+1][0],mas[i][0] + mas[i][1],mas[i+1][1] = mas[i+1][1],mas[i][1] +for i in range( len(mas)): + + print(mas[i][0], mas[i][1]) From 7b0f999ecc0d0b021e3f09bec213b10784b8f18b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=83=D0=B3=D0=B0=D1=81=D0=BE=D0=B2=20=D0=A1=D1=82?= =?UTF-8?q?=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Mon, 8 Mar 2021 23:32:14 +0300 Subject: [PATCH 04/24] task 1 --- src/bubble_sort.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/bubble_sort.py diff --git a/src/bubble_sort.py b/src/bubble_sort.py new file mode 100644 index 0000000..fd1349c --- /dev/null +++ b/src/bubble_sort.py @@ -0,0 +1,20 @@ +n= int(input()) +arr = input() +str_lst = arr.split() +res = [] +num = True +for item in str_lst: + res.append(int(item)) + +n = len(res) +for i in range (0, n-1): + #print(i) + for j in range(0, n-i-1): + #print(j) + if res[j] > res[j+1]: + num = False + res[j],res[j+1] = res[j+1],res[j] + print(" ".join(map(str, res))) + +if num == True: + print(0) \ No newline at end of file From 33de2b567a669153cf844696f37d9aa7d3b16a2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=83=D0=B3=D0=B0=D1=81=D0=BE=D0=B2=20=D0=A1=D1=82?= =?UTF-8?q?=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Mon, 8 Mar 2021 23:37:00 +0300 Subject: [PATCH 05/24] task 6 --- src/storage.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/storage.py diff --git a/src/storage.py b/src/storage.py new file mode 100644 index 0000000..3dc5713 --- /dev/null +++ b/src/storage.py @@ -0,0 +1,16 @@ + +N = int(input()) +m = list(map(int, input().split(" "))) +k = int(input()) +t = list(map(int, input().split(" "))) +def counting_sort(m, t, n): + temp = [0]*(n+1) + for item in t: + temp[item] += 1 + for i in range(n): + if m[i] >= temp[i+1]: + print("no") + else: + print("yes") + +counting_sort(m, t, N) \ No newline at end of file From baab04bbcaa51d924d9ce5bd679c5cc8339b8d14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=83=D0=B3=D0=B0=D1=81=D0=BE=D0=B2=20=D0=A1=D1=82?= =?UTF-8?q?=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Mon, 8 Mar 2021 23:38:26 +0300 Subject: [PATCH 06/24] task 5 --- src/nums_sort.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/nums_sort.py diff --git a/src/nums_sort.py b/src/nums_sort.py new file mode 100644 index 0000000..6de80a4 --- /dev/null +++ b/src/nums_sort.py @@ -0,0 +1,8 @@ +n = int(input()) +m = list(map(int, input().split())) +x = 0 +m.sort() +for i in range(n-1): + if m[i] != m[i+1]: + x +=1 +print(x+1) From 1964f7533e1c0a76b697e9c747b275175185d408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=83=D0=B3=D0=B0=D1=81=D0=BE=D0=B2=20=D0=A1=D1=82?= =?UTF-8?q?=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Wed, 10 Mar 2021 21:07:45 +0300 Subject: [PATCH 07/24] task 5(change) --- src/fifth.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/fifth.py diff --git a/src/fifth.py b/src/fifth.py new file mode 100644 index 0000000..7c8073e --- /dev/null +++ b/src/fifth.py @@ -0,0 +1,8 @@ +n = int(input()) +m = list(map(int, input().split(" "))) +sort ={} +for i in m: + if i not in sort: + sort[i] = 1 + +print(len(sort)) From 400302de5d4fec8de2547287ba60bb87f973b0bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=83=D0=B3=D0=B0=D1=81=D0=BE=D0=B2=20=D0=A1=D1=82?= =?UTF-8?q?=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Wed, 10 Mar 2021 21:39:41 +0300 Subject: [PATCH 08/24] task 3 and 7 --- src/merger_sort.py | 35 +++++++++++++++++++++++++++++++++++ src/radix_sort.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/merger_sort.py create mode 100644 src/radix_sort.py diff --git a/src/merger_sort.py b/src/merger_sort.py new file mode 100644 index 0000000..858eac9 --- /dev/null +++ b/src/merger_sort.py @@ -0,0 +1,35 @@ + +def merger(mas, List): + new_list = [] + i, j = 0, 0 + while i < len(mas) and j < len(List): + if mas[i] < List[j]: + new_list.append(mas[i]) + i += 1 + else: + new_list.append(List[j]) + j += 1 + while i < len(mas): + new_list.append(mas[i]) + i += 1 + while j < len(List): + new_list.append(List[j]) + j += 1 + + return new_list +def merger_sort(mas, index): + if len(mas) < 2: + return mas + else: + mid = len(mas)//2 + left = merger_sort(mas[:mid], [index[0],index[0]+mid]) + right = merger_sort(mas[mid:],[index[0]+mid, index[1]]) + + sort = merger(left, right) + print(index[0]+1, index[1], sort[0], sort[-1]) + return sort +Size = int(input()) +mas = list(map(int, input().split(" "))) + +mas = merger_sort(mas,[0, Size]) +print(' '.join(map(str, mas))) \ No newline at end of file diff --git a/src/radix_sort.py b/src/radix_sort.py new file mode 100644 index 0000000..3c62e58 --- /dev/null +++ b/src/radix_sort.py @@ -0,0 +1,29 @@ +def radix_sort(L, n): + mas = len(L(0)) + length = len(str(max(L))) + for i in range(mas): + arr = [[] for j in range(10)] + L=[] + for item in L: + index = int(item)//10**i % 10 + arr[index].append(item) + print("Phase 1", i+1) + for i in range(10): + print(f"Bucket {i}: ", end="") + print(", ".join(arr[i]) if len(arr[i])>0 else "empty") + L = L + arr[i] + print("**********") + return L + +N = int(input()) +L = [] +for i in range(N): + L.append(input()) +print("Initial array:") +print(", ".join(L)) +print("**********") +L = radix_sort(L, N) +print("Sorted array:") +print(", ".join(L)) + + From 55900c3213485bd8b42dd0fb951bd3039c8e726f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=83=D0=B3=D0=B0=D1=81=D0=BE=D0=B2=20=D0=A1=D1=82?= =?UTF-8?q?=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Sat, 13 Mar 2021 17:26:35 +0300 Subject: [PATCH 09/24] task 3 and 7(changed) --- src/task3.py | 34 ++++++++++++++++++++++++++++++++++ src/task7.py | 26 ++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/task3.py create mode 100644 src/task7.py diff --git a/src/task3.py b/src/task3.py new file mode 100644 index 0000000..ecd50b7 --- /dev/null +++ b/src/task3.py @@ -0,0 +1,34 @@ +def merger(mas, List): + new_list = [] + i, j = 0, 0 + while i < len(mas) and j < len(List): + if mas[i] < List[j]: + new_list.append(mas[i]) + i += 1 + else: + new_list.append(List[j]) + j += 1 + while i < len(mas): + new_list.append(mas[i]) + i += 1 + while j < len(List): + new_list.append(List[j]) + j += 1 + + return new_list +def merger_sort(mas, index): + if len(mas) < 2: + return mas + else: + mid = len(mas)//2 + left = merger_sort(mas[:mid], [index[0],index[0]+mid]) + right = merger_sort(mas[mid:],[index[0]+mid, index[1]]) + + sort = merger(left, right) + print(index[0]+1, index[1], sort[0], sort[-1]) + return sort +Size = int(input()) +mas = list(map(int, input().split())) + +mas = merger_sort(mas,[0, Size]) +print(" ".join(map(str, mas))) diff --git a/src/task7.py b/src/task7.py new file mode 100644 index 0000000..d44f652 --- /dev/null +++ b/src/task7.py @@ -0,0 +1,26 @@ +def radix_sort(L, n): + mas = len(L[0]) + for i in range(mas): + radix_arr = [[] for j in range(10)] + for item in L: + index = int(item)//10**i % 10 + radix_arr[index].append(item) + print("Phase", i+1) + L = [] + for i in range(10): + print("Bucket ", i ,":", sep = "", end=" ") + print(", ".join(radix_arr[i]) if len(radix_arr[i])>0 else "empty") + L = L + radix_arr[i] + print("**********") + return L + +n = int(input()) +L = [] +for i in range(n): + L.append(input()) +print("Initial array:") +print(", ".join(L)) +print("**********") +L = radix_sort(L, n) +print("Sorted array:") +print(", ".join(L)) \ No newline at end of file From b7d1a0a240cc44246ce52173afb6e9b898c206fd Mon Sep 17 00:00:00 2001 From: Stepan_Bugasov <72213274+Bugfasov@users.noreply.github.com> Date: Mon, 5 Apr 2021 17:40:30 +0300 Subject: [PATCH 10/24] 1_3 --- src/hash.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/hash.py diff --git a/src/hash.py b/src/hash.py new file mode 100644 index 0000000..6ac5026 --- /dev/null +++ b/src/hash.py @@ -0,0 +1,29 @@ +def substring_search(S, T): + substring_str = 0 + for i in range(T): + substring_str = (substring_str * p + ord(S[i])) % q + return substring_str + +def Rabin_Karp_Matcher(str_substring, pattern, x, y): + lst = [] + str_substring = hash(y) + pattern_substring = hash(y) + pt = 1 + for i in range(x-1): + pt = (pt * p) % q + for i in range(1, x-y+2): + if str_substring == pattern_substring: + lst.append(i-1) + if i < x-y+1: + str_substring = (str_substring - ord(str_substring[i-1]) * pt) * p + str_substring += ord(str_substring[i + y - 1]) + str_substring %= q + return lst +string = input() +pattern_string = input() +global p +p = 31 +global q +q = 2 ** 31 - 1 +A = Rabin_Karp_Matcher(string, pattern_string, len(string), len(pattern_string)) +print(' '.join(map(str, A))) \ No newline at end of file From ed507f594dd9a2cc963e7fdfce986c494d10e7f5 Mon Sep 17 00:00:00 2001 From: Stepan_Bugasov <72213274+Bugfasov@users.noreply.github.com> Date: Wed, 21 Apr 2021 12:49:10 +0300 Subject: [PATCH 11/24] mod3_task1 --- src/righthash.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/righthash.py diff --git a/src/righthash.py b/src/righthash.py new file mode 100644 index 0000000..fe6a889 --- /dev/null +++ b/src/righthash.py @@ -0,0 +1,22 @@ +def substring_search(m): + S = [0]*len(m) + for i in range(len(m) - 1): + j = S[i] + while j > 0 and (m[i+1] != m[j]): + j = S[j-1] + if m[i+1] == m[j]: + S[i+1] = j+1 + else: + S[i+1] = 0 + return S + +def main(): + k = input() + t = input() + t_k = t + '&' + k + S = substring_search(t_k) + for i in range(len(S)): + if S[i] == len(t): + print(i-2*len(t), end = ' ') + +main() \ No newline at end of file From 409431d645bd1e6dd5c032008d99ab0d2c0f5ade Mon Sep 17 00:00:00 2001 From: Stepan_Bugasov <72213274+Bugfasov@users.noreply.github.com> Date: Wed, 21 Apr 2021 13:02:25 +0300 Subject: [PATCH 12/24] mo3_task2 --- src/hash_hash.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/hash_hash.py diff --git a/src/hash_hash.py b/src/hash_hash.py new file mode 100644 index 0000000..f0826a4 --- /dev/null +++ b/src/hash_hash.py @@ -0,0 +1,36 @@ +def cyclic_shift(c,x,y): + hash = 0 + for i in range(len(c)): + hash = (hash*x+ord(c[i]))%y + return hash + +def function(c,t,x,y): + if c!=t: + arr = 1 + t = t[1:] + t[0] + c_s = cyclic_shift(c,x,y) + t_s = cyclic_shift(t,x,y) + k = 1 + for i in range(len(c)-1): + k=(k*x)%y + for i in range(len(t)-1): + if c_s == t_s: + break + else: + t_s = (x*(t_s-ord(t[i])*k) + ord(t[i]))%y + arr += 1 + if c_s == t_s: + print(arr) + else: + print(-1) + else: + print(0) + +def main(): + c = input() + t = input() + x = 30 + y = 1e9+6 + a = function(c,t,x,y) + +main() \ No newline at end of file From 8d23b50117e3352f755954e2229326f4bba654f1 Mon Sep 17 00:00:00 2001 From: Stepan_Bugasov <72213274+Bugfasov@users.noreply.github.com> Date: Sun, 2 May 2021 11:38:36 +0300 Subject: [PATCH 13/24] mod3_task3 --- src/line_period.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/line_period.py diff --git a/src/line_period.py b/src/line_period.py new file mode 100644 index 0000000..9f861ce --- /dev/null +++ b/src/line_period.py @@ -0,0 +1,23 @@ +def line_period(s): + x = [0] * len(s) + for i in range(1, len(s)): + k = x[i-1] + while (k > 0 and s[k] != s[i]): + k = x[k - 1] + if s[i] == s[k]: + k += 1 + x[i] = k + return x + +def main(): + s = input() + s_t = s + '&' + s + L = line_period(s_t) + lr = L[-1]-L[len(s)-1] + k = len(s)//lr + if s[:lr]*k == s: + print(k) + else: + print(1) + +main() \ No newline at end of file From 82833c582384fa2864c6eba38596f9a9573ab13b Mon Sep 17 00:00:00 2001 From: Stepan_Bugasov <72213274+Bugfasov@users.noreply.github.com> Date: Sat, 15 May 2021 10:50:19 +0300 Subject: [PATCH 14/24] mod3_task4 --- src/Looped_string.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/Looped_string.py diff --git a/src/Looped_string.py b/src/Looped_string.py new file mode 100644 index 0000000..3318377 --- /dev/null +++ b/src/Looped_string.py @@ -0,0 +1,16 @@ +def looped_string(S, n): + res = [0]*n + res[0] = 0 + for i in range(n-1): + x = res[i] + while (x > 0 and S[i+1] != S[x]): + x = res[x-1] + if (S[i+1] == S[x]): + res[i+1] = x + 1 + else: + res[i+1] = 0 + return res + +T = input() +S = looped_string(T, len(T)) +print(len(T) - S[len(S)-1]) \ No newline at end of file From 42de9bb7e36bec6bcb96740b6eb168bd65d8ad6c Mon Sep 17 00:00:00 2001 From: Stepan_Bugasov <72213274+Bugfasov@users.noreply.github.com> Date: Thu, 27 May 2021 18:57:07 +0300 Subject: [PATCH 15/24] mod4_task1 --- src/Psp.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/Psp.py diff --git a/src/Psp.py b/src/Psp.py new file mode 100644 index 0000000..d3c597d --- /dev/null +++ b/src/Psp.py @@ -0,0 +1,14 @@ +def Psp(s): + Counter=0 + myStack = [] + for c in s: + if (c == '('): + myStack.append(c) + elif (myStack != []) and (myStack[-1] == '('): + myStack.pop() + else: + Counter += 1 + return Counter+len(myStack) + +s = str(input()) +print(Psp(s)) \ No newline at end of file From f2f1f80eb4186cb62f73addc059c9b3075e34fc2 Mon Sep 17 00:00:00 2001 From: Stepan_Bugasov <72213274+Bugfasov@users.noreply.github.com> Date: Thu, 27 May 2021 19:45:10 +0300 Subject: [PATCH 16/24] mod4_task2 --- src/trains.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/trains.py diff --git a/src/trains.py b/src/trains.py new file mode 100644 index 0000000..6135718 --- /dev/null +++ b/src/trains.py @@ -0,0 +1,23 @@ +l = int(input()) +s = list(map(int, input().split())) + +deadlock = [0] +wayB = [0] + +for i in range(l): + while deadlock[-1] == wayB[-1] + 1: + wayB.append(deadlock[-1]) + deadlock.pop() + if s[i] == wayB[-1] + 1: + wayB.append(s[i]) + else: + deadlock.append(s[i]) + +while deadlock[-1] == wayB[-1] + 1: + wayB.append(deadlock[-1]) + deadlock.pop() + +if wayB[-1] == l: + print('YES') +else: + print('NO') From 1f9648647e8b720a5c164593398b28c96b3ae4d7 Mon Sep 17 00:00:00 2001 From: Stepan_Bugasov <72213274+Bugfasov@users.noreply.github.com> Date: Fri, 28 May 2021 10:04:53 +0300 Subject: [PATCH 17/24] mod4_task3 --- src/nums.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/nums.py diff --git a/src/nums.py b/src/nums.py new file mode 100644 index 0000000..a6b4ac0 --- /dev/null +++ b/src/nums.py @@ -0,0 +1,23 @@ +def closest_smaller(S, n): + stack = [n-1] + index = [0]*n + index[n-1] = -1 + for i in range(n-2, -1, -1): + if S[stack[-1]] >= S[i]: + while (stack != []) and (S[stack[-1]] >= S[i]): + stack.pop() + if (stack == []): + index[i] = -1 + else: + index[i] = stack[-1] + stack.append(i) + else: + index[i] = stack[-1] + stack.append(i) + return index + + +N = int(input()) +S = list(map(int, input().split())) +S = closest_smaller(S, N) +print(' '.join(map(str, S))) \ No newline at end of file From 5f8de093e892ac1657b33385783861c47a67a43c Mon Sep 17 00:00:00 2001 From: Stepan_Bugasov <72213274+Bugfasov@users.noreply.github.com> Date: Sat, 29 May 2021 17:31:26 +0300 Subject: [PATCH 18/24] mod4_task4 --- src/window.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/window.py diff --git a/src/window.py b/src/window.py new file mode 100644 index 0000000..b3db034 --- /dev/null +++ b/src/window.py @@ -0,0 +1,17 @@ +def Segment_minima(n, m_w, s): + stack = [] + for i in range(m_w): + while stack and s[i] <= s[stack[-1]]: + stack.pop() + stack.append(i) + for i in range(m_w, n): + print(s[stack[0]]) + while stack and i - m_w >= stack[0]: + stack.pop(0) + while stack and s[i] <= s[stack[-1]]: + stack.pop() + stack.append(i) + print(s[stack[0]]) +N, K = map(int, input().split()) +s = list(map(int, input().split())) +Segment_minima(N, K, s) \ No newline at end of file From 729bf7f205d82034747aafc82e0eddd5f1684abb Mon Sep 17 00:00:00 2001 From: Stepan_Bugasov <72213274+Bugfasov@users.noreply.github.com> Date: Sat, 29 May 2021 17:56:29 +0300 Subject: [PATCH 19/24] mod2_task4 --- src/inversion.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/inversion.py diff --git a/src/inversion.py b/src/inversion.py new file mode 100644 index 0000000..f0f7fe5 --- /dev/null +++ b/src/inversion.py @@ -0,0 +1,35 @@ +def invers_fun(num): + n = len(num) + if n <= 1: + return num, 0 + mid = n//2 + left, left_inv = invers_fun(num[:mid]) + right, right_inv = invers_fun(num[mid:]) + cout, s = sort(left, right) + m = s + left_inv + right_inv + return cout, m + +def sort(left, right): + s = 0 + cout = [] + i, j = 0, 0 + while i < len(left) and j < len(right): + if left[i] <= right[j]: + cout.append(left[i]) + i += 1 + else: + cout.append(right[j]) + j += 1 + s += len(left) - i + while i < len(left): + cout.append(left[i]) + i += 1 + while j < len(right): + cout.append(right[j]) + j += 1 + return cout, s + +n = int(input()) +num = list(map(int, input().split(" "))) +num, inversions = invers_fun(num) +print(inversions) \ No newline at end of file From 0b5676d7b34954bea51c795afbccb98903dce064 Mon Sep 17 00:00:00 2001 From: Stepan_Bugasov <72213274+Bugfasov@users.noreply.github.com> Date: Sun, 30 May 2021 19:48:43 +0300 Subject: [PATCH 20/24] mod5_task1 --- src/tree.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/tree.py diff --git a/src/tree.py b/src/tree.py new file mode 100644 index 0000000..cdf580b --- /dev/null +++ b/src/tree.py @@ -0,0 +1,55 @@ +class key: + __size = 0 + __height = 0 + + def __init__(trees, leaf): + trees.leaf = leaf + trees.left = None + trees.right = None + key.__size += 1 + + @property + def size(trees): + return trees.__size + + @property + def height(trees): + return trees.__height + + def add(trees, meaning, elevation): + if trees.leaf == meaning: + return + if meaning < trees.leaf: + if trees.left: + trees.left.add(meaning, elevation+1) + else: + if key.__height < elevation: + key.__height = elevation + trees.left = key(meaning) + else: + if trees.right: + trees.right.add(meaning, elevation+1) + else: + if key.__height < elevation: + key.__height = elevation + trees.right = key(meaning) + + def find_divarication(trees): + if trees.left: + trees.left.find_divarication() + if trees.left and not trees.right: + print(trees.leaf) + if trees.right and not trees.left: + print(trees.leaf) + if trees.right: + trees.right.find_divarication() + +def binary_tree(objct): + tree = key(objct[0]) + for i in range(1, len(objct)-1): + tree.add(objct[i], 1) + return tree + +lisst = list(map(int, input().split())) +tree = binary_tree(lisst) +tree.find_divarication() \ No newline at end of file From b55785838e52f4a7e58d9a52bc12e187b1354539 Mon Sep 17 00:00:00 2001 From: Stepan_Bugasov <72213274+Bugfasov@users.noreply.github.com> Date: Wed, 2 Jun 2021 21:16:12 +0300 Subject: [PATCH 21/24] mod5_task2 --- src/balanced_tree.py | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/balanced_tree.py diff --git a/src/balanced_tree.py b/src/balanced_tree.py new file mode 100644 index 0000000..04a5818 --- /dev/null +++ b/src/balanced_tree.py @@ -0,0 +1,48 @@ +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.right: + self.right.add(data) + else: + self.right = Node(data) + else: + if self.left: + self.left.add(data) + else: + self.left = Node(data) + +def elevation(tree): + if tree is None: + return 0 + return max(elevation(tree.right), elevation(tree.left))+1 + +def binary_tree(el): + base = Node(el[0]) + for i in range(1, len(el)): + base.add(el[i]) + return base + +def balance(tree): + if not tree or ((elevation(tree.right) == elevation(tree.left) or elevation(tree.right)+1 == elevation(tree.left) or elevation(tree.right) == elevation(tree.left)+1) + and balance(tree.right) and balance(tree.left)): + return 1 + return 0 + +def main(): + n = list(map(int,input().split())) + n.pop() + tree = binary_tree(n) + + if balance(tree) == 1: + print("YES") + else: + print("NO") + +main() \ No newline at end of file From 153eb814ae962facaa19b3ec2250719c06f35da4 Mon Sep 17 00:00:00 2001 From: Stepan_Bugasov <72213274+Bugfasov@users.noreply.github.com> Date: Wed, 2 Jun 2021 21:52:50 +0300 Subject: [PATCH 22/24] mod5_task3 --- src/greatest_common_factor.py | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/greatest_common_factor.py diff --git a/src/greatest_common_factor.py b/src/greatest_common_factor.py new file mode 100644 index 0000000..9738dac --- /dev/null +++ b/src/greatest_common_factor.py @@ -0,0 +1,40 @@ +def g_c_f(b, lft, rght, s_leaf, arr): + if rght - lft == 1: + s_leaf[b] = arr[lft] + return + m = (rght + lft) // 2 + g_c_f(2 * b + 1, lft, m, s_leaf, arr) + g_c_f(2 * b + 2, m, rght, s_leaf, arr) + s_leaf[b] = GCF(s_leaf[2 * b + 1], s_leaf[2 * b + 2]) + +def GCF(p,b): + while b: + p, b = b, p%b + return p + +def get_GCF(b, lft, rght, s_leaf, left, right): + if left <= lft and right >= rght: + return s_leaf[b] + if left >= rght or right <= lft: + return 0 + m = (rght + lft) // 2 + st_right = get_GCF(2 * b + 2, m, rght, s_leaf, left, right) + st_left = get_GCF(2 * b + 1, lft, m, s_leaf, left, right) + return GCF(st_left, st_right) + + +def main(): + n = int(input()) + arr = list(map(int, input().split()))[:n] + s_leaf = [0] * 4 * n + g_c_f(0, 0, n, s_leaf, arr) + q = int(input()) + p = [] + + while q != 0: + lft, rght = map(int, input().split()) + p.append(get_GCF(0, 0, n, s_leaf, lft - 1, rght)) + q -= 1 + print(*p) + +main() \ No newline at end of file From 5f11ce02ec65ea699cccc401a5f192fa08aa474a Mon Sep 17 00:00:00 2001 From: Stepan_Bugasov <72213274+Bugfasov@users.noreply.github.com> Date: Wed, 2 Jun 2021 22:17:39 +0300 Subject: [PATCH 23/24] mod5_task4 --- src/subsegment.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/subsegment.py diff --git a/src/subsegment.py b/src/subsegment.py new file mode 100644 index 0000000..f5920ea --- /dev/null +++ b/src/subsegment.py @@ -0,0 +1,48 @@ +def subsegment(b, lft, rght, s_sub, p): + if rght-lft == 1: + s_sub[b] = p[lft] + return + m = (rght+lft)//2 + subsegment(2*b+1, lft, m, s_sub, p) + subsegment(2*b+2, m, rght, s_sub, p) + s_sub[b] = s_sub[2*b+1] + s_sub[2*b+2] + +def rummage(b, lft, rght, s_sub, t): + if t > s_sub[b]: + return -1 + if rght - lft == 1: + return rght + m = (rght+lft)//2 + if s_sub[2*b+1] >= t: + return rummage(2*b+1, lft, m, s_sub, t) + else: + return rummage(2*b+2, m, rght, s_sub, t - s_sub[2*b+1]) + +def get_sum(b, lft, rght, s_sub, ql, qr): + if ql <= lft and qr >= rght: + return s_sub[b] + if ql >= rght or qr <= lft: + return 0 + m = (rght+lft)//2 + s_left = get_sum(2*b+1, lft, m, s_sub, ql, qr) + s_right = get_sum(2*b+2, m, rght, s_sub, ql, qr) + return s_left + s_right +def main(): + arr = int(input()) + p = [0 if int(i) != 0 else 1 for i in input().split()] + s_sub = [0]*4*arr + subsegment(0, 0, arr, s_sub, p) + q = int(input()) + index = [] + while q != 0: + lft, rght, t = map(int, input().split()) + sum = get_sum(0, 0, arr, s_sub, lft-1, rght) + if sum >= t and lft > 1: + index.append(rummage(0, 0, arr, s_sub, t+get_sum(0, 0, arr, s_sub, 0, lft-1))) + elif sum >= t and lft == 1: + index.append(rummage(0, 0, arr, s_sub, t)) + else: + index.append(-1) + q -= 1 + print(*index) +main() \ No newline at end of file From ac33e77d516263b33112c0f6995a6bfa299cf7dc Mon Sep 17 00:00:00 2001 From: Stepan_Bugasov <72213274+Bugfasov@users.noreply.github.com> Date: Wed, 2 Jun 2021 22:59:00 +0300 Subject: [PATCH 24/24] mod5_task5-6 --- src/dynamic_version.py | 50 +++++++++++++++++++++++++++++++ src/sub_din.py | 67 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/dynamic_version.py create mode 100644 src/sub_din.py diff --git a/src/dynamic_version.py b/src/dynamic_version.py new file mode 100644 index 0000000..e44e95d --- /dev/null +++ b/src/dynamic_version.py @@ -0,0 +1,50 @@ +from math import gcd + +def dynamic_version(val, x, r, num, p): + if r - x == 1: + num[val] = p[x] + return + + middle = (x + r)//2 + dynamic_version(2*val + 1, x, middle, num, p) + dynamic_version(2*val + 2, middle, r, num, p) + num[val] = gcd(num[2*val+1], num[2*val+2]) + +def calc(val, x, r, it, q_x, q_r): + if q_x <= x and q_r >= r: + return it[val] + if q_x >= r or q_r <= x: + return 0 + + middle = (x + r)//2 + tx = calc(2*val + 1, x, middle, it, q_x, q_r) + tr = calc(2*val + 2, middle, r, it, q_x, q_r) + return gcd(tx, tr) + +def up(val, x, r, num, idx, value): + if r - x == 1: + num[val] = value + return + middle = (r+x)//2 + if idx < middle: + up(val*2+1, x, middle, num, idx, value) + else: + up(val*2+2, middle, r, num, idx, value) + num[val] = gcd(num[2*val+1], num[2*val+2]) + +def main(): + t = int(input()) + num = [0] * (4 * t) + p = list(map(int, input().split()))[:t] + dynamic_version(0, 0, t, num, p) + q = int(input()) + ct = [] + while q != 0: + t_q, x, r = map(str, input().split()) + if t_q == 's': + ct.append(calc(0, 0, t, num, int(x)-1, int(r))) + else: + up(0, 0, t, num, int(x)-1, int(r)) + q -= 1 + print(*ct) +main() \ No newline at end of file diff --git a/src/sub_din.py b/src/sub_din.py new file mode 100644 index 0000000..f06ee8b --- /dev/null +++ b/src/sub_din.py @@ -0,0 +1,67 @@ +def sub_din(val, x, r, s_sub, p): + if r-x == 1: + s_sub[val] = p[x] + return + m = (r+x)//2 + sub_din(2*val+1, x, m, s_sub, p) + sub_din(2*val+2, m, r, s_sub, p) + s_sub[val] = s_sub[2*val+1] + s_sub[2*val+2] + +def search(val, x, r, s_sub, t): + if t > s_sub[val]: + return -1 + if r - x == 1: + return r + m = (r+x)//2 + if s_sub[2*val+1] >= t: + return search(2*val+1, x, m, s_sub, t) + else: + return search(2*val+2, m, r, s_sub, t - s_sub[2*val+1]) + +def get_sum(val, x, r, s_sub, qx, qr): + if qx <= x and qr >= r: + return s_sub[val] + if qx >= r or qr <= x: + return 0 + m = (r+x)//2 + tx = get_sum(2*val+1, x, m, s_sub, qx, qr) + tr = get_sum(2*val+2, m, r, s_sub, qx, qr) + return tx + tr + +def up(val, x, r, s_sub, indx, value): + if r-x == 1: + s_sub[val] = value + return + m = (r+x)//2 + if indx < m: + up(2*val+1, x, m, s_sub, indx, value) + else: + up(2*val+2, m, r, s_sub, indx, value) + s_sub[val] = s_sub[2*val+1] + s_sub[2*val+2] +def main(): + n = int(input()) + p = [0 if int(i) != 0 else 1 for i in input().split()] + s_sub = [0]*4*n + sub_din(0, 0, n, s_sub, p) + q = int(input()) + ind = [] + while q != 0: + i = input().split() + if len(i) == 4: + x, r, t = int(i[1]), int(i[2]), int(i[3]) + cout_ = get_sum(0, 0, n, s_sub, x-1, r) + if cout_ >= t and x > 1: + ind.append(search(0, 0, n, s_sub, t+get_sum(0, 0, n, s_sub, 0, x-1))) + elif cout_ >= t and x == 1: + ind.append(search(0, 0, n, s_sub, t)) + else: + ind.append(-1) + else: + i, val = int(i[1]), int(i[2]) + if val == 0: + up(0, 0, n, s_sub, i-1, 1) + else: + up(0, 0, n, s_sub, i-1, 0) + q -= 1 + print(*ind) +main() \ No newline at end of file