From 7a15959e95e58eb92b1e004de48f1899a473ffd0 Mon Sep 17 00:00:00 2001 From: piglaker Date: Sun, 13 Sep 2020 13:25:58 +0800 Subject: [PATCH] add PTA of ZJU_MOOC in Python & part of AdvancedLevel PAT in Python --- AdvancedLevel_Python/1001.py | 26 +++++++ AdvancedLevel_Python/1002.py | 37 +++++++++ AdvancedLevel_Python/1003.py | 35 +++++++++ AdvancedLevel_Python/1004.py | 35 +++++++++ AdvancedLevel_Python/1005.py | 7 ++ AdvancedLevel_Python/1006.py | 23 ++++++ AdvancedLevel_Python/1007.py | 37 +++++++++ AdvancedLevel_Python/1008.py | 35 +++++++++ AdvancedLevel_Python/1009.py | 30 ++++++++ AdvancedLevel_Python/1011.py | 15 ++++ AdvancedLevel_Python/1012.py | 39 ++++++++++ AdvancedLevel_Python/1013.py | 49 ++++++++++++ AdvancedLevel_Python/1062.py | 26 +++++++ AdvancedLevel_Python/1087.py | 109 +++++++++++++++++++++++++++ AdvancedLevel_Python/1088.py | 62 +++++++++++++++ AdvancedLevel_Python/1089.py | 54 ++++++++++++++ AdvancedLevel_Python/1090.py | 50 +++++++++++++ AdvancedLevel_Python/1131.py | 116 +++++++++++++++++++++++++++++ AdvancedLevel_Python/1132.py | 11 +++ AdvancedLevel_Python/1133.py | 35 +++++++++ AdvancedLevel_Python/1134.py | 22 ++++++ AdvancedLevel_Python/1136.py | 13 ++++ AdvancedLevel_Python/1137.py | 42 +++++++++++ AdvancedLevel_Python/1138.py | 14 ++++ AdvancedLevel_Python/1139.py | 49 ++++++++++++ AdvancedLevel_Python/1140.py | 17 +++++ AdvancedLevel_Python/1141.py | 39 ++++++++++ AdvancedLevel_Python/1142.py | 35 +++++++++ AdvancedLevel_Python/1143.py | 33 ++++++++ AdvancedLevel_Python/1144.py | 11 +++ AdvancedLevel_Python/1145.py | 75 +++++++++++++++++++ AdvancedLevel_Python/1146.py | 27 +++++++ AdvancedLevel_Python/1147.py | 95 +++++++++++++++++++++++ AdvancedLevel_Python/1149.py | 34 +++++++++ AdvancedLevel_Python/1150.py | 57 ++++++++++++++ AdvancedLevel_Python/1151.py | 41 ++++++++++ AdvancedLevel_Python/1152.py | 20 +++++ AdvancedLevel_Python/1153.py | 52 +++++++++++++ AdvancedLevel_Python/1154.py | 24 ++++++ AdvancedLevel_Python/1155.py | 79 ++++++++++++++++++++ AdvancedLevel_Python/README.md | 2 + AdvancedLevel_Python/pat_exam/1.py | 26 +++++++ AdvancedLevel_Python/pat_exam/2.py | 64 ++++++++++++++++ AdvancedLevel_Python/pat_exam/3.py | 32 ++++++++ AdvancedLevel_Python/pat_exam/4.py | 37 +++++++++ PTA_MOOC_Python/README.md | 80 ++++++++++++++++++++ PTA_MOOC_Python/amp.py | 28 +++++++ PTA_MOOC_Python/src01.py | 20 +++++ PTA_MOOC_Python/src02.py | 40 ++++++++++ PTA_MOOC_Python/src03.py | 83 +++++++++++++++++++++ PTA_MOOC_Python/src04.py | 31 ++++++++ PTA_MOOC_Python/src05.py | 35 +++++++++ PTA_MOOC_Python/src06.py | 77 +++++++++++++++++++ PTA_MOOC_Python/src07.py | 50 +++++++++++++ PTA_MOOC_Python/src08.py | 26 +++++++ PTA_MOOC_Python/src08_.py | 44 +++++++++++ PTA_MOOC_Python/src09.py | 25 +++++++ PTA_MOOC_Python/src10.py | 59 +++++++++++++++ PTA_MOOC_Python/src11.py | 38 ++++++++++ PTA_MOOC_Python/src12.py | 20 +++++ PTA_MOOC_Python/src13.py | 54 ++++++++++++++ PTA_MOOC_Python/src14.py | 59 +++++++++++++++ PTA_MOOC_Python/src15.py | 57 ++++++++++++++ PTA_MOOC_Python/src16.py | 60 +++++++++++++++ PTA_MOOC_Python/src17.py | 34 +++++++++ PTA_MOOC_Python/src18.py | 31 ++++++++ PTA_MOOC_Python/src19.py | 78 +++++++++++++++++++ PTA_MOOC_Python/src20.py | 30 ++++++++ PTA_MOOC_Python/src21.py | 23 ++++++ PTA_MOOC_Python/src22.py | 51 +++++++++++++ PTA_MOOC_Python/src23.py | 110 +++++++++++++++++++++++++++ PTA_MOOC_Python/src24.py | 4 + PTA_MOOC_Python/src25.py | 65 ++++++++++++++++ PTA_MOOC_Python/src26.py | 62 +++++++++++++++ PTA_MOOC_Python/src27.py | 10 +++ PTA_MOOC_Python/src28.py | 54 ++++++++++++++ PTA_MOOC_Python/src29.py | 22 ++++++ PTA_MOOC_Python/src30.py | 24 ++++++ PTA_MOOC_Python/src31.py | 45 +++++++++++ PTA_MOOC_Python/src32.py | 20 +++++ PTA_MOOC_Python/src33.py | 28 +++++++ 81 files changed, 3348 insertions(+) create mode 100644 AdvancedLevel_Python/1001.py create mode 100644 AdvancedLevel_Python/1002.py create mode 100644 AdvancedLevel_Python/1003.py create mode 100644 AdvancedLevel_Python/1004.py create mode 100644 AdvancedLevel_Python/1005.py create mode 100644 AdvancedLevel_Python/1006.py create mode 100644 AdvancedLevel_Python/1007.py create mode 100644 AdvancedLevel_Python/1008.py create mode 100644 AdvancedLevel_Python/1009.py create mode 100644 AdvancedLevel_Python/1011.py create mode 100644 AdvancedLevel_Python/1012.py create mode 100644 AdvancedLevel_Python/1013.py create mode 100644 AdvancedLevel_Python/1062.py create mode 100644 AdvancedLevel_Python/1087.py create mode 100644 AdvancedLevel_Python/1088.py create mode 100644 AdvancedLevel_Python/1089.py create mode 100644 AdvancedLevel_Python/1090.py create mode 100644 AdvancedLevel_Python/1131.py create mode 100644 AdvancedLevel_Python/1132.py create mode 100644 AdvancedLevel_Python/1133.py create mode 100644 AdvancedLevel_Python/1134.py create mode 100644 AdvancedLevel_Python/1136.py create mode 100644 AdvancedLevel_Python/1137.py create mode 100644 AdvancedLevel_Python/1138.py create mode 100644 AdvancedLevel_Python/1139.py create mode 100644 AdvancedLevel_Python/1140.py create mode 100644 AdvancedLevel_Python/1141.py create mode 100644 AdvancedLevel_Python/1142.py create mode 100644 AdvancedLevel_Python/1143.py create mode 100644 AdvancedLevel_Python/1144.py create mode 100644 AdvancedLevel_Python/1145.py create mode 100644 AdvancedLevel_Python/1146.py create mode 100644 AdvancedLevel_Python/1147.py create mode 100644 AdvancedLevel_Python/1149.py create mode 100644 AdvancedLevel_Python/1150.py create mode 100644 AdvancedLevel_Python/1151.py create mode 100644 AdvancedLevel_Python/1152.py create mode 100644 AdvancedLevel_Python/1153.py create mode 100644 AdvancedLevel_Python/1154.py create mode 100644 AdvancedLevel_Python/1155.py create mode 100644 AdvancedLevel_Python/README.md create mode 100644 AdvancedLevel_Python/pat_exam/1.py create mode 100644 AdvancedLevel_Python/pat_exam/2.py create mode 100644 AdvancedLevel_Python/pat_exam/3.py create mode 100644 AdvancedLevel_Python/pat_exam/4.py create mode 100644 PTA_MOOC_Python/README.md create mode 100644 PTA_MOOC_Python/amp.py create mode 100644 PTA_MOOC_Python/src01.py create mode 100644 PTA_MOOC_Python/src02.py create mode 100644 PTA_MOOC_Python/src03.py create mode 100644 PTA_MOOC_Python/src04.py create mode 100644 PTA_MOOC_Python/src05.py create mode 100644 PTA_MOOC_Python/src06.py create mode 100644 PTA_MOOC_Python/src07.py create mode 100644 PTA_MOOC_Python/src08.py create mode 100644 PTA_MOOC_Python/src08_.py create mode 100644 PTA_MOOC_Python/src09.py create mode 100644 PTA_MOOC_Python/src10.py create mode 100644 PTA_MOOC_Python/src11.py create mode 100644 PTA_MOOC_Python/src12.py create mode 100644 PTA_MOOC_Python/src13.py create mode 100644 PTA_MOOC_Python/src14.py create mode 100644 PTA_MOOC_Python/src15.py create mode 100644 PTA_MOOC_Python/src16.py create mode 100644 PTA_MOOC_Python/src17.py create mode 100644 PTA_MOOC_Python/src18.py create mode 100644 PTA_MOOC_Python/src19.py create mode 100644 PTA_MOOC_Python/src20.py create mode 100644 PTA_MOOC_Python/src21.py create mode 100644 PTA_MOOC_Python/src22.py create mode 100644 PTA_MOOC_Python/src23.py create mode 100644 PTA_MOOC_Python/src24.py create mode 100644 PTA_MOOC_Python/src25.py create mode 100644 PTA_MOOC_Python/src26.py create mode 100644 PTA_MOOC_Python/src27.py create mode 100644 PTA_MOOC_Python/src28.py create mode 100644 PTA_MOOC_Python/src29.py create mode 100644 PTA_MOOC_Python/src30.py create mode 100644 PTA_MOOC_Python/src31.py create mode 100644 PTA_MOOC_Python/src32.py create mode 100644 PTA_MOOC_Python/src33.py diff --git a/AdvancedLevel_Python/1001.py b/AdvancedLevel_Python/1001.py new file mode 100644 index 0000000..75075ce --- /dev/null +++ b/AdvancedLevel_Python/1001.py @@ -0,0 +1,26 @@ +n, m = list(map(int, input().split())) + +ans = str(n + m) + +def get_list(ans): + a, b = len(ans) / 3, int(len(ans) % 3) + if not b == 0: + re = [ans[:b]] + else: + re = [] + for i in range(0, int(a)): + re.append(ans[b + 3 * i:b + i * 3 + 3]) + return re + +if len(ans) < 4: + print(ans) +else: + if int(ans) < 0: + re = get_list(ans[1:]) + tmp = ','.join(re) + print('-' + tmp) + + else: + re = get_list(ans) + print(','.join(re)) + diff --git a/AdvancedLevel_Python/1002.py b/AdvancedLevel_Python/1002.py new file mode 100644 index 0000000..2707314 --- /dev/null +++ b/AdvancedLevel_Python/1002.py @@ -0,0 +1,37 @@ +p = [] + +p.append(input().split()) + +p.append(input().split()) + +p_dict = [{int(x[2 * i + 1]) : float(x[2 * i+2]) for i in range(int(x[0]))} for x in p] + +final = {} + +for d in p_dict: + for key in d.keys(): + if not key in final: + final[key] = d[key] + else: + final[key] += d[key] + +s = '' + +tmp = [] + +final_sorted_keys = sorted(final, key=lambda x:-x) + +k = 0 + +for i in iter(final_sorted_keys): + if not final[i] == 0: + tmp.append(str(i)) + tmp.append(str(round(final[i]* 10) / 10)) + k += 1 +if k != 0: + s += str(k) + ' ' +else: + s += str(k) +s += ' '.join(tmp) + +print(s) diff --git a/AdvancedLevel_Python/1003.py b/AdvancedLevel_Python/1003.py new file mode 100644 index 0000000..ff1ff50 --- /dev/null +++ b/AdvancedLevel_Python/1003.py @@ -0,0 +1,35 @@ +n, m = list(map(int, input().split())) + +data = [] + +if n == 0 : + exit(0) + +for i in range(m): + data.append(input().split()) + +def build_tree(data): + tree = {} + for i in data: + tree[i[0]] = i[2:] + return tree + +def deeper(tree): + head = '01' + layer = [head] + ans = [] + while layer: + num_of_leaves = 0 + next_layer = [] + for node in layer: + if not node in tree.keys(): + num_of_leaves += 1 + else: + son_of_node = tree[node] + next_layer += son_of_node + ans.append(num_of_leaves) + layer = next_layer + + return ans + +print(' '.join(list(map(str, deeper(build_tree(data)))))) diff --git a/AdvancedLevel_Python/1004.py b/AdvancedLevel_Python/1004.py new file mode 100644 index 0000000..ff1ff50 --- /dev/null +++ b/AdvancedLevel_Python/1004.py @@ -0,0 +1,35 @@ +n, m = list(map(int, input().split())) + +data = [] + +if n == 0 : + exit(0) + +for i in range(m): + data.append(input().split()) + +def build_tree(data): + tree = {} + for i in data: + tree[i[0]] = i[2:] + return tree + +def deeper(tree): + head = '01' + layer = [head] + ans = [] + while layer: + num_of_leaves = 0 + next_layer = [] + for node in layer: + if not node in tree.keys(): + num_of_leaves += 1 + else: + son_of_node = tree[node] + next_layer += son_of_node + ans.append(num_of_leaves) + layer = next_layer + + return ans + +print(' '.join(list(map(str, deeper(build_tree(data)))))) diff --git a/AdvancedLevel_Python/1005.py b/AdvancedLevel_Python/1005.py new file mode 100644 index 0000000..54db9ab --- /dev/null +++ b/AdvancedLevel_Python/1005.py @@ -0,0 +1,7 @@ +n = input() + +s = str(sum([int(i) for i in n])) + +database = ['zero', 'one', 'two','three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] + +print(' '.join([database[int(j)] for j in s])) diff --git a/AdvancedLevel_Python/1006.py b/AdvancedLevel_Python/1006.py new file mode 100644 index 0000000..1b8a637 --- /dev/null +++ b/AdvancedLevel_Python/1006.py @@ -0,0 +1,23 @@ +k = int(input()) + +diary = [] + +def date2second(date): + h,m,s = date.split(':') + return 3600 * h + 60 * m + s + +for i in range(k): + record = input().split() + diary.append(record) + +diary.sort(key=lambda x:date2second(x[1])) + +first_bird = diary[0][0] + +diary.sort(key=lambda x:date2second(x[2])) + +last_one = diary[-1][0] + +print(first_bird + ' ' + last_one) + + diff --git a/AdvancedLevel_Python/1007.py b/AdvancedLevel_Python/1007.py new file mode 100644 index 0000000..ec53d21 --- /dev/null +++ b/AdvancedLevel_Python/1007.py @@ -0,0 +1,37 @@ +k = int(input()) + +nums = list(map(int, input().split())) + +max_arr = [] +max_ = 0 + +tmp = 0 +tmp_arr = [] + +for i in range(len(nums)): + + if tmp > 0: + tmp += nums[i] + + tmp_arr.append(nums[i]) + else: + tmp = nums[i] + + tmp_arr = [tmp] + + if tmp > max_: + max_ = tmp + + from copy import deepcopy + + max_arr.append(deepcopy(tmp_arr)) + +if max_ > 0: + for arr in max_arr: + if sum(arr) == max_: + print(max_, arr[0], arr[-1]) + exit(0) +elif max_ == 0: + print(0,0,0) +else: + print(0, nums[0], nums[-1]) diff --git a/AdvancedLevel_Python/1008.py b/AdvancedLevel_Python/1008.py new file mode 100644 index 0000000..b76d354 --- /dev/null +++ b/AdvancedLevel_Python/1008.py @@ -0,0 +1,35 @@ +k = int(input()) + +nums = list(map(int, input().split())) + +max_arr = [] +max_ = 0 + +tmp = 0 +tmp_arr = [] + +for i in range(len(nums)): + + if tmp > 0: + tmp += nums[i] + + tmp_arr.append(nums[i]) + else: + tmp = nums[i] + + tmp_arr = [tmp] + + if tmp >= max_: + max_ = tmp + + from copy import deepcopy + + max_arr.append(deepcopy(tmp_arr)) + +if max_ > 0: + for arr in max_arr: + if sum(arr) == max_: + print(max_, arr[0], arr[-1]) + exit(0) +else: + print(0, nums[0], nums[-1]) diff --git a/AdvancedLevel_Python/1009.py b/AdvancedLevel_Python/1009.py new file mode 100644 index 0000000..2d8ae95 --- /dev/null +++ b/AdvancedLevel_Python/1009.py @@ -0,0 +1,30 @@ +a = list(map(float, input().split())) + +b = list(map(float, input().split())) + +arr1 = [[a[2*i +1], a[2*i+2]] for i in range(int(a[0]))] + +arr2 = [[b[2*i + 1], b[2*i+2]] for i in range(int(b[0]))] + +def conv(arr1, arr2): + + matrix = {} + + for x in arr1: + def dot(x, arr2): + ans = [] + for y in arr2: + ans.append([x[0]+y[0],x[1]*y[1]]) + return ans + + vector = dot(x, arr2) + + for z in vector: + if z[0] in matrix: + matrix[z[0]] += z[1] + else: + matrix[z[0]] = z[1] + matrix_ = sorted(matrix, key=lambda x:-x) + return str(len([key for key in iter(matrix) if matrix[key] != 0 ])) + ' ' + ''.join([str(int(key)) + ' ' + str(round(matrix[key] *10) / 10) +' ' for key in matrix_ if matrix[key] != 0]) + +print(conv(arr1, arr2)[:-1]) diff --git a/AdvancedLevel_Python/1011.py b/AdvancedLevel_Python/1011.py new file mode 100644 index 0000000..0bbb976 --- /dev/null +++ b/AdvancedLevel_Python/1011.py @@ -0,0 +1,15 @@ +def myread(): + return list(map(float, input().split())) + + +games = [myread() for i in range(3)] + +diary = ['W', 'T', "L"] + +ans = ' '.join([diary[i] for i in [x.index(max(x)) for x in games]]) + +from functools import reduce + +total = str(round((reduce(lambda x, y:x*y, [max(i) for i in games]) * 0.65 - 1) * 2 * 100) / 100) + +print(ans + ' ' + total) diff --git a/AdvancedLevel_Python/1012.py b/AdvancedLevel_Python/1012.py new file mode 100644 index 0000000..856d2e6 --- /dev/null +++ b/AdvancedLevel_Python/1012.py @@ -0,0 +1,39 @@ +def myread(): + return list(map(int, input().split())) + +n, m = myread() + +database = {} + +scores_rank = {'C':[], 'M':[],'E':[],'A':[]} + +for i in range(n): + tmp = myread() + database[str(tmp[0])] = [sum(tmp[1:])] + tmp[1:] + scores_rank['C'].append(tmp[1]) + scores_rank['M'].append(tmp[2]) + scores_rank['E'].append(tmp[3]) + scores_rank['A'].append(sum(tmp[1:])) + +for key in scores_rank: + scores_rank[key].sort(reverse=True) + +query = [] + +diary = [ 'A', 'C', 'M', 'E'] + +for i in range(m): + tmp = input() + if not tmp in database.keys(): + print('N/A') + else: + finished = False + for k in range(n): + if not finished: + for j in range(4): + if scores_rank[diary[j]][k] <= database[tmp][j]: + print(str(k+1) + ' ' + diary[j]) + finished = True + break + else: + break diff --git a/AdvancedLevel_Python/1013.py b/AdvancedLevel_Python/1013.py new file mode 100644 index 0000000..8445206 --- /dev/null +++ b/AdvancedLevel_Python/1013.py @@ -0,0 +1,49 @@ +def myread(): + return list(map(int, input().split())) + + +n, m, k = myread() + +graph = [[-1 for i in range(n)] for j in range(n)] + +for i in range(m): + tmp = myread() + graph[tmp[0] - 1][tmp[1] - 1 ] = 1 + graph[tmp[1] - 1][tmp[0] - 1] = 1 + +query = myread() + + +def wander(graph, bad, his): + + total = [] + + def walk(graph, s, bad, his): + + his.append(s) + tmp = [s] + layer = [s] + while layer: + next_layer = [] + for node in layer: + for i in range(n): + if graph[node][i] == 1 and i not in his and i != bad and i not in layer: + next_layer.append(i) + his.append(i) + tmp.append(i) + layer = next_layer + + return tmp + + for i in range(n): + if i != bad and i not in his: + total.append(walk(graph, i, bad, his)) + + return len(total) - 1 if len(total) >= 1 else 0 + + +for ruined_city in query: + his = [] + print(wander(graph, ruined_city - 1, his)) + + diff --git a/AdvancedLevel_Python/1062.py b/AdvancedLevel_Python/1062.py new file mode 100644 index 0000000..6e4d3e4 --- /dev/null +++ b/AdvancedLevel_Python/1062.py @@ -0,0 +1,26 @@ +[a,bottom,top] = list(map(int, input().split())) + +sages, noble, fool, rest = [], [], [], [] +result = [sages, noble, fool,rest] + +for i in range(a): + man = list(map(int, input().split())) + + if man[1] >= top and man[2] >= top: + sages.append(man+[man[2]+man[1]]) + elif man[2] < top and man[1] >= top and man[2] >= bottom: + noble.append(man+[man[2]+man[1]]) + elif man[1] < top and man[2] < top and man[1] >= man[2] and man[2] >= bottom: + fool.append(man+[man[2]+man[1]]) + elif man[1] >= bottom and man[2] >= bottom: + rest.append(man+[man[2]+man[1]]) +k = 0 + +for i in result:k+= len(i) + +print(k) + +for group in result: + group.sort(key = lambda x:(x[1] + x[2]) * 100 + x[1] - x[0] * 0.000000001, reverse=True) + for i in range(len(group)): + print(' '.join(str(ele) for ele in group[i][0:3])) diff --git a/AdvancedLevel_Python/1087.py b/AdvancedLevel_Python/1087.py new file mode 100644 index 0000000..81daa21 --- /dev/null +++ b/AdvancedLevel_Python/1087.py @@ -0,0 +1,109 @@ +n, k, s = input().split() + +city = {} + +number2city = [s] + +city[s] = 0 + +for i in range(int(n) - 1): + tmp = input().split() + city[tmp[0]] = int(tmp[1]) + + if tmp[0] not in number2city: + number2city.append(tmp[0]) + +city2number = {number2city[i]:i for i in range(len(number2city))} + +matrix = [[10000000 for j in range(int(n))]for i in range(int(n))] + +for i in range(int(k)): + + tmp = input().split() + + matrix[city2number[tmp[0]]][city2number[tmp[1]]] = int(tmp[2]) + + matrix[city2number[tmp[1]]][city2number[tmp[0]]] = int(tmp[2]) + + +def djk(matrix, s, d): + + count = [1 for i in range(int(n))] + + his =[s] + + distance = [matrix[i][s] for i in range(int(n)) ] + + values = [city[number2city[i]] for i in range(int(n))] + + min_city_number = [-1 for i in range(int(n))] + + min_city_number[s] = 1 + + pre_senquence = [[] for i in range(int(n))] + + pre_senquence[s].append(s) + + while len(his) != int(n): + min_ = 1000000 + next_ = 0 + for i in range(int(n)): + if distance[i] < min_ and i not in his: + min_ = distance[i] + next_ = i + + for past in his: + if matrix[past][next_] == min_: + count[next_] = count[past] + min_city_number[next_] = min_city_number[past] + 1 + pre_senquence[next_].append(past) + + his.append(next_) + + for i in range(int(n)): + if i not in his : + if matrix[next_][i] + distance[next_] < distance[i]: + distance[i] = distance[next_] + matrix[i][next_] + values[i] = city[number2city[i]] + values[next_] + count[i] = count[next_] + min_city_number[i] = min_city_number[next_] + 1 + pre_senquence[i].clear() + pre_senquence[i].append(next_) + + elif matrix[i][next_] + distance[next_] == distance[i]: + count[i] += count[next_] + if city[number2city[i]] + values[next_] > values[i] : + values[i] = city[number2city[i]] + values[next_] + min_city_number[i] = min_city_number[next_] + 1 + pre_senquence[i].clear() + pre_senquence[i].append(next_) + elif city[number2city[i]] + values[next_] == values[i]: + if min_city_number[next_] + 1 < min_city_number[i]: + min_city_number[i] = min_city_number[next_] + 1 + pre_senquence[i].clear() + pre_senquence[i].append(next_) + + return count[d], distance[d], values[d], pre_senquence, min_city_number + + +count, dis, happiness, pre_senquence, min_city_number = djk(matrix, city2number[s], city2number['ROM']) + + +def dfs(pre_senquence, s, d): + ans = [d] + now = d + while now != s: + now = pre_senquence[now][0] + ans.append(now) + + + return ans + +net = dfs(pre_senquence, city2number[s], city2number['ROM']) + +avg_happiness = happiness // (min_city_number[city2number['ROM']] - 1) + +print(str(count) + ' ' + str(dis) + ' ' + str(happiness) + ' '+ str(avg_happiness)) + +print('->'.join(list(map(str, [number2city[i] for i in net[::-1]])))) + diff --git a/AdvancedLevel_Python/1088.py b/AdvancedLevel_Python/1088.py new file mode 100644 index 0000000..ba9dc01 --- /dev/null +++ b/AdvancedLevel_Python/1088.py @@ -0,0 +1,62 @@ +a, b = input().split() + +a1, a2 = list(map(int, a.split('/'))) +b1, b2 = list(map(int, b.split('/'))) + +min_k = a2 * b2 + +ans1, ans2= a1 * b2, a2 * b1 + +def div(top, bottom): + while 1: + stop = True + for i in range(2, int(top)+1): + if int(bottom) % i == 0 and int(top) % i ==0: + bottom /= int(i) + top /= int(i) + stop = False + break + if stop: + return int(top), int(bottom) + + +def simplify(top, bottom): + if top % bottom == 0: + ans = int(top / bottom) + + return '('+str(ans) +')' if ans < 0 else str(ans) + + if abs(top) < abs(bottom): + a_, b_ = div(abs(top), abs(bottom)) + ans = str(a_) + '/' + str(b_) + return '(' +'-' + ans +')' if top * bottom < 0 else ans + + if abs(top) > abs(bottom): + k0 = str(abs(top) // abs(bottom)) if top * bottom > 0 else '-' +str(abs(top) // abs(bottom)) + k1 = str(abs(top) % abs(bottom)) + + a_, b_ = div(int(k1), bottom) + ans = k0 +' '+ str(a_) + '/' + str(b_) + return '(' + ans +')' if top * bottom < 0 else ans + +in_a, in_b = simplify(a1,a2), simplify(b1,b2) + +re1 = simplify(ans1 + ans2, min_k) +print(in_a + ' + ' + in_b +' = ' + re1) + +re2 = simplify(ans1 - ans2, min_k) +print(in_a + ' - ' + in_b + ' = ' + re2) + +re3 = a1 * b1 +re3_ = a2 * b2 + +print(in_a + ' * ' + in_b + ' = ' + simplify(re3, re3_)) + +re4 = a1 * b2 +re4_ = a2 * b1 + +if re4_ == 0: + print(in_a + ' / ' + in_b + ' = ' + 'Inf') +else: + print(in_a + ' / ' + in_b + ' = ' + simplify(re4, re4_)) + diff --git a/AdvancedLevel_Python/1089.py b/AdvancedLevel_Python/1089.py new file mode 100644 index 0000000..cfb7d20 --- /dev/null +++ b/AdvancedLevel_Python/1089.py @@ -0,0 +1,54 @@ +n = int(input()) + +arr_o = list(map(int, input().split())) + +arr_n= list(map(int, input().split())) + +def next_insert_sort(i, tmp, arr_o): + ans = deepcopy(tmp) + for j in range(len(ans)): + if arr_o[i] < ans[j]: + ans.insert(j, arr_o[i]) + return ans + ans.append(arr_o[i]) + return ans + +def next_merge_sort(i, now): + def merge(arr1, arr2): + tmp = arr1 + arr2 + tmp.sort() + return tmp + import math + step = int(math.pow(2, i-1)) + tmp = [] + for j in range(0, len(now), step * 2): + tmp += merge(now[j:j+step], now[j+step:j+step*2]) + return tmp + +tmp1 = [] +from copy import deepcopy +tmp2 = deepcopy(arr_o) +is_insert = False +is_merge = False + +for i in range(1, n): + tmp1 = next_insert_sort(i-1, tmp1, arr_o) + + tmp2 = next_merge_sort(i, tmp2) + + if is_insert: + print(' '.join(list(map(str, tmp1 + arr_o[i:])))) + exit(0) + if is_merge: + print(' '.join(list(map(str, tmp2)))) + exit(0) + if tmp1 + arr_o[i:] == arr_n: + print('Insertion Sort') + is_insert = True + if tmp2 == arr_n: + print('Merge Sort') + is_merge = True + + + + diff --git a/AdvancedLevel_Python/1090.py b/AdvancedLevel_Python/1090.py new file mode 100644 index 0000000..382a85e --- /dev/null +++ b/AdvancedLevel_Python/1090.py @@ -0,0 +1,50 @@ +n, p, r = list(map(float, input().split())) + +arr = list(map(int, input().split())) + +root = arr.index(-1) + +k = 0 +start = 0 +k_list = [] +his = [] + +k_array = [-2 for i in range(int(n))] + +for i in range(int(n)): + if i not in his: + k = 0 + next_node = i + his.append(next_node) + while next_node != -1: + next_node = arr[next_node] + if k_array[next_node] != -2: + k += 1 + k += k_array[next_node] + break + else: + k += 1 + his.append(next_node) + k_array[i] = k + k_list.append(k) + +from math import pow + +top = max(k_list) + +ans = pow(1 + 0.01*r, top-1) * p + +k = 0 +for i in k_list: + if i == top: + k += 1 + +from math import modf + +a, b = modf(ans * 100) + +if a >= 0.5: + print(str((b+1) / 100) +' ' + str(k) ) +else: + print(str(b / 100) +' ' + str(k)) + diff --git a/AdvancedLevel_Python/1131.py b/AdvancedLevel_Python/1131.py new file mode 100644 index 0000000..58d35f7 --- /dev/null +++ b/AdvancedLevel_Python/1131.py @@ -0,0 +1,116 @@ +n = int(input()) + +data = [] + +Number_of_station = {} + +Station_of_number = {} + +note = 0 + +for i in range(n): + tmp = input().split() + data.append(tmp[1:]) + +for line in data: + for station in line: + if station in Number_of_station.keys(): + pass + else: + Number_of_station[station] = note + note += 1 + +for station in Number_of_station.keys(): + Station_of_number[Number_of_station[station]] = station + +graph = [['' for i in range(note)] for i in range(note)] + +m = int(input()) + +plans = [] + +for i in range(m): plans.append(input().split()) + +for i in range(len(data)): + line = data[i] + + for j in range(len(line) - 1): + graph[Number_of_station[line[j]]][Number_of_station[line[j + 1]]] = 'Line#' + str(i+1) + graph[Number_of_station[line[j + 1]]][Number_of_station[line[j]]] = 'Line#' + str(i+1) + +def floyd(graph, layer, destination, his, route, boat, cost): + if not layer: return + layer_pro = [] + route_pro = [] + for station in layer: + for number in range(len(graph[Number_of_station[station]])): + if (graph[Number_of_station[station]][number]) and (Station_of_number[number] not in his): + layer_pro.append(Station_of_number[number]) + his.append(Station_of_number[number]) + + for solution in route: + if solution[-1] == station: + route_pro.append(solution + [Station_of_number[number]]) + new_line = graph[Number_of_station[station]][number] + if len(solution) > 2: + old_line = graph[Number_of_station[solution[-2]]][Number_of_station[solution[-1]]] + if new_line != old_line: + route_pro[-1][0] += 1 + cost[Station_of_number[number]] = solution[0] + 1 + else: + cost[Station_of_number[number]] = solution[0] + else: + cost[Station_of_number[number]] = solution[0] + else: + route_pro.append(solution) + else: + pass + + if route_pro: + for i in route_pro: + if not i in boat: boat.append(i) + + #print(cost) + #print(layer_pro, route) + + layer_pro.sort(key=lambda x:cost[x]) + + floyd(graph, layer_pro, destination, his, route_pro, boat, cost) + + +for task in plans: + his = [task[0]] + route = [[0,task[0]]] + boat = [] + cost = {} + floyd(graph, [task[0]], task[1], his, route, boat, cost) + + boat.sort(key=lambda x:len(x)) + #[print(i) for i in boat] + boat_ = [] + for re in boat: + if re[-1] == task[1]: + boat_.append(re) + boat_.sort(key= lambda x:x[0]) + + def station2line(graph, line): + ans = [] + + tmp = 'Take ' + graph[Number_of_station[line[1]]][Number_of_station[line[2]]] + ' from ' + str(line[1]) + ' to ' + + now_line = graph[Number_of_station[line[1]]][Number_of_station[line[2]]] + + for i in range(2, len(line)-1): + #print(i, len(line) - 1, line[i]) + if graph[Number_of_station[line[i]]][Number_of_station[line[i + 1]]] != now_line: + ans.append(tmp + str(line[i]) + '.') + tmp = 'Take ' + graph[Number_of_station[line[i]]][Number_of_station[line[i + 1]]] + ' from ' + str( + line[i]) + ' to ' + now_line = graph[Number_of_station[line[i]]][Number_of_station[line[i + 1]]] + if i == len(line) - 2: + ans.append(tmp + str(line[i+1]) + '.') + #print(tmp) + return ans + print(len(boat_[0]) - 2) + [print(duan) for duan in station2line(graph, boat_[0])] + diff --git a/AdvancedLevel_Python/1132.py b/AdvancedLevel_Python/1132.py new file mode 100644 index 0000000..8dd9db2 --- /dev/null +++ b/AdvancedLevel_Python/1132.py @@ -0,0 +1,11 @@ +n = int(input()) + +for i in range(n): + tmp = input() + left, right = int(tmp[:len(tmp)//2]), int(tmp[len(tmp)//2:]) + if left == 0 or right == 0: + print('No') + elif int(tmp) % (left * right) == 0: + print('Yes') + else: + print('No') diff --git a/AdvancedLevel_Python/1133.py b/AdvancedLevel_Python/1133.py new file mode 100644 index 0000000..7909ab4 --- /dev/null +++ b/AdvancedLevel_Python/1133.py @@ -0,0 +1,35 @@ +start, n, k = input().split() + +data = {} + +for i in range(int(n)): + tmp = input().split() + data[tmp[0]] = [int(tmp[1]), tmp[2]] + +data_pro = [] + +p = start + +while p != '-1': + data_pro.append([p] + data[p]) + p = data[p][1] + +ne = [] +po = [] +gr_k = [] + +for i in data_pro: + if i[1] < 0: + ne.append(i) + elif i[1] > int(k): + gr_k.append(i) + else: + po.append(i) + +ans = ne + po + gr_k + +for i in range(len(data_pro) - 1): + tmp = str(ans[i][0]) + ' ' + str(ans[i][1]) + ' ' + str(ans[i + 1][0]) + print(tmp) + +print(str(ans[-1][0]) + ' ' + str(ans[-1][1]) + ' ' + '-1') diff --git a/AdvancedLevel_Python/1134.py b/AdvancedLevel_Python/1134.py new file mode 100644 index 0000000..d5d2061 --- /dev/null +++ b/AdvancedLevel_Python/1134.py @@ -0,0 +1,22 @@ +length, n = list(map(int, input().split())) + +edges = [] + +for i in range(n): + edges.append(list(map(int, input().split()))) + +k = int(input()) + +ans = [] + +for i in range(k): + tmp = list(map(int, input().split()))[1:] + index = True + for edge in edges: + if (edge[0] not in tmp) and (edge[1] not in tmp): + index = False + break + if index: ans.append('Yes') + else: ans.append('No') + +[print(i) for i in ans] diff --git a/AdvancedLevel_Python/1136.py b/AdvancedLevel_Python/1136.py new file mode 100644 index 0000000..e668fd6 --- /dev/null +++ b/AdvancedLevel_Python/1136.py @@ -0,0 +1,13 @@ +n = int(input()) + +for i in range(10): + if str(n) == str(n)[::-1]: + print(str(n) + ' is a palindromic number.') + exit(0) + print(str(n) + ' + ' + str(n)[::-1] + ' = ' + str(n + int(str(n)[::-1]))) + n = n + int(str(n)[::-1]) + if str(n) == str(n)[::-1]: + print(str(n) + ' is a palindromic number.') + exit(0) + +print('Not found in 10 iterations.') diff --git a/AdvancedLevel_Python/1137.py b/AdvancedLevel_Python/1137.py new file mode 100644 index 0000000..457abe8 --- /dev/null +++ b/AdvancedLevel_Python/1137.py @@ -0,0 +1,42 @@ +P, M, N = list(map(int, input().split())) + +Gp = {} + +for i in range(P): + tmp = input().split() + Gp[tmp[0]] = int(tmp[1]) + +Gm = {} + +for i in range(M): + tmp = input().split() + Gm[tmp[0]] = int(tmp[1]) + +Gf = {} + +for i in range(N): + tmp = input().split() + Gf[tmp[0]] = int(tmp[1]) + +list_of_names = {} + +for name in Gp.keys(): + if Gp[name] >= 200: + list_of_names[name] = str(Gp[name]) + +for name in list_of_names: + if not name in Gm: Gm[name] = -1 + if not name in Gf: Gf[name] = -1 + if max(round((Gm[name]*2 + Gf[name]*3) / 5), Gf[name]) >= 60: + list_of_names[name] = [list_of_names[name], Gm[name], Gf[name], max(round((Gm[name]*2 + Gf[name]*3) / 5), Gf[name])] + +ans = [] + +for key in list_of_names.keys(): + if len(list_of_names[key]) == 4: + ans.append([key] + list_of_names[key]) + +ans.sort(key=lambda x:(-x[4], x[0] )) + +for student in ans: + print(' '.join(list(map(str, student)))) diff --git a/AdvancedLevel_Python/1138.py b/AdvancedLevel_Python/1138.py new file mode 100644 index 0000000..0f8c3d1 --- /dev/null +++ b/AdvancedLevel_Python/1138.py @@ -0,0 +1,14 @@ +n = input() + +preord = list(map(int, input().split())) + +inord = list(map(int, input().split())) + + +def cycle(preord, inord): + if not preord: return [] + if len(preord) == 1: return preord + key = inord.index(preord[0]) + return cycle(preord[1:key+1], inord[:key]) + cycle(preord[key+1:], inord[key+1:]) + [preord[0]] + +print(str(cycle(preord, inord)[0])) diff --git a/AdvancedLevel_Python/1139.py b/AdvancedLevel_Python/1139.py new file mode 100644 index 0000000..28b1c84 --- /dev/null +++ b/AdvancedLevel_Python/1139.py @@ -0,0 +1,49 @@ +n, m = list(map(int, input().split())) + +guys = {} + +query = [] + +for i in range(m): + tmp = input().split() + + def add(d, tmp): + for i in range(2): + if tmp[i] in d.keys(): + d[tmp[i]].append(tmp[abs(i-1)]) + else: + d[tmp[i]] = [tmp[abs(i-1)]] + add(guys, tmp) + +k = int(input()) +for i in range(k): query.append(input().split()) + + +for couple in query: + ans = [] + for lover in couple: + if not lover in guys.keys(): + guys[lover] = [] + + layer = [list(set([guy for guy in guys[lover] if (len(guy) == len(lover) and [guy] != [man for man in couple if man != lover]) ] )) for lover in couple] + + one_side = {medium:[man for man in guys[medium] if man != couple[1]] for medium in set(layer[0])} + + for e in set(layer[1]): + + for key in one_side.keys(): + if e in one_side[key]: + ans.append([abs(int(key)), abs(int(e))]) + + ans.sort(key=lambda x:(int(x[0]), int(x[1]))) + print(len(ans)) + def decorate(s): + ans = '' + if len(s) != 4: + for i in range(4-len(s)): + ans += '0' + return ans + s + + [print(' '.join(map(decorate, list(map(str, i))))) for i in ans] + + diff --git a/AdvancedLevel_Python/1140.py b/AdvancedLevel_Python/1140.py new file mode 100644 index 0000000..20cb3cc --- /dev/null +++ b/AdvancedLevel_Python/1140.py @@ -0,0 +1,17 @@ +d, m = input().split() + +ans = d + +def next(s): + tmp = [] + for i in range(len(s)): + if tmp and (s[i] in tmp[-1]): + tmp[-1].append(s[i]) + else: + tmp.append([s[i]]) + return ''.join([str(each[0]) + str(len(each)) for each in tmp]) + +for i in range(int(m) - 1): + ans = next(ans) + +print(ans) diff --git a/AdvancedLevel_Python/1141.py b/AdvancedLevel_Python/1141.py new file mode 100644 index 0000000..9997989 --- /dev/null +++ b/AdvancedLevel_Python/1141.py @@ -0,0 +1,39 @@ +n = int(input()) + +TWS_of_School = {} + +for i in range(n): + tmp = input().split() + + def tws_of_tmp(tmp): + if tmp[0][0] == 'B': + return int(tmp[1]) * 2 + elif tmp[0][0] == 'A': + return int(tmp[1]) * 3 + else: + return int(tmp[1]) * 4.5 + + if not tmp[-1].lower() in TWS_of_School.keys(): + TWS_of_School[tmp[-1].lower()] = [tws_of_tmp(tmp)] + else: + TWS_of_School[tmp[-1].lower()].append(tws_of_tmp(tmp)) + + +ans = [ [school_name, TWS_of_School[school_name]] for school_name in iter(TWS_of_School)] + +ans.sort(key=lambda x : ( -sum(x[1]), len(x[1]), x[0] )) + +k = 1 +count = 0 +pre_TWS = 100000000000000 + +print(len(ans)) + +for line in ans: + tws = int(sum(line[1]) // 3) + if tws < pre_TWS: + k += count + count = 0 + pre_TWS = tws + count += 1 + print(str(k) + ' ' + line[0] + ' ' + str(tws) + ' ' + str(len(line[1]))) diff --git a/AdvancedLevel_Python/1142.py b/AdvancedLevel_Python/1142.py new file mode 100644 index 0000000..96a042a --- /dev/null +++ b/AdvancedLevel_Python/1142.py @@ -0,0 +1,35 @@ +Nv, Ne = list(map(int, input().split())) + +graph = [[] for i in range(Nv)] + +for i in range(Ne): + tmp = list(map(int, input().split())) + graph[tmp[1] - 1].append(tmp[0]) + graph[tmp[0] - 1].append(tmp[1]) + +M = int(input()) + +ans = [] + + +def judge(tmp, graph): + for i in range(len(tmp)): + for j in range(i + 1, len(tmp)): + if not tmp[j] in graph[tmp[i] - 1]: + return 'Not a Clique' + + from functools import reduce + if reduce(lambda x,y: x & y,[set(graph[point - 1]) for point in tmp]) - set(tmp): + return 'Not Maximal' + else: + return 'Yes' + +ans = [] +for _ in range(M): + tmp = list(map(int, input().split()))[1:] + ans.append(judge(tmp, graph)) + +[print(_) for _ in ans] + + + diff --git a/AdvancedLevel_Python/1143.py b/AdvancedLevel_Python/1143.py new file mode 100644 index 0000000..fa87b4e --- /dev/null +++ b/AdvancedLevel_Python/1143.py @@ -0,0 +1,33 @@ +n, m = list(map(int, input().split())) + +preord = list(map(int, input().split())) + +task_set = [] + +for i in range(n): + task_set.append(list(map(int, input().split()))) + +ans = [] + +def solve(preord, task): + if (not task[0] in preord) and (not task[1] in preord): + return 'ERROR: ' + str(task[0]) + ' and ' + str(task[1]) + ' are not found.' + elif not task[0] in preord: + return 'ERROR: ' + str(task[0]) +' is not found.' + elif not task[1] in preord: + return 'ERROR: ' + str(task[1]) +' is not found.' + else: + for node in preord: + if (node - task[0]) * (node - task[1]) < 0: + return 'LCA of ' + str(task[0]) + ' and ' + str(task[1]) + ' is ' + str(node) + '.' + elif node - task[0] == 0: + return str(task[0]) + ' is an ancestor of '+ str(task[1]) + '.' + elif node - task[1] == 0: + return str(task[1]) + ' is an ancestor of ' + str(task[0]) + '.' + +for task in task_set: + ans.append(solve(preord, task)) + + +[print(i) for i in ans] + diff --git a/AdvancedLevel_Python/1144.py b/AdvancedLevel_Python/1144.py new file mode 100644 index 0000000..8eac905 --- /dev/null +++ b/AdvancedLevel_Python/1144.py @@ -0,0 +1,11 @@ +n = input() + +arr = list(map(int, input().split())) +k = 1 +while 1: + if not k in arr: + print(k) + exit(0) + else: + k += 1 + diff --git a/AdvancedLevel_Python/1145.py b/AdvancedLevel_Python/1145.py new file mode 100644 index 0000000..edb3ba9 --- /dev/null +++ b/AdvancedLevel_Python/1145.py @@ -0,0 +1,75 @@ +def get_prime(Msize): + number = Msize + + def is_prime(number): + for i in range(2, number): + if number % i == 0: + return False + else: + return True + + while 1: + if is_prime(number): + return number + else: + number += 1 + +Msize, M, N = list(map(int, input().split())) + +arr = list(map(int, input().split())) + +task = list(map(int, input().split())) + +hashing = {} + +Msize = get_prime(Msize) + +def insert_hash(e, hashing): + k = 1 + key = e % Msize + if not key in hashing.keys(): + hashing[key] = e + return [k] + elif hashing[key] == e: + return [k] + else: + success = False + for i in range(1, Msize): + key_tmp = (key + i ** 2) % Msize + k += 1 + if not key_tmp in hashing.keys(): + hashing[key_tmp] = e + success = True + break + elif hashing[key_tmp] != e : + pass + else: + return [k] + if not success : + k += 1 + return k, str(e) + ' cannot be inserted.' + else: + return [k] + +for e in arr: + insert_hash(e, hashing) +ans = 0 + +for e in task: + from copy import deepcopy + hashing_ = deepcopy(hashing) + tmp = insert_hash(e, hashing_) + if len(tmp) > 1: + print(tmp[1]) + ans += tmp[0] + + +from math import modf + +a, b = modf(ans / len(task) * 10) + +if a >= 0.5: + print((b / 10 + 0.1)) +else: + print(b / 10) + diff --git a/AdvancedLevel_Python/1146.py b/AdvancedLevel_Python/1146.py new file mode 100644 index 0000000..c150151 --- /dev/null +++ b/AdvancedLevel_Python/1146.py @@ -0,0 +1,27 @@ +n, m = list(map(int, input().split())) + +prenode_of_nodei = {i+1:[] for i in range(n)} + +for i in range(m): + tmp = list(map(int, input().split())) + prenode_of_nodei[tmp[1]].append(tmp[0]) + +k = int(input()) + +def is_wrong(prenode_of_nodei, query): + + for i in range(len(query)): + pre_set, post_set = query[:i], query[i+1:] + for prenode in prenode_of_nodei[query[i]]: + if not prenode in pre_set: + return True + return False + +ans = [] + +for i in range(k): + query = list(map(int, input().split())) + if is_wrong(prenode_of_nodei, query): + ans.append(str(i)) + +print(' '.join(ans)) diff --git a/AdvancedLevel_Python/1147.py b/AdvancedLevel_Python/1147.py new file mode 100644 index 0000000..dc34ffb --- /dev/null +++ b/AdvancedLevel_Python/1147.py @@ -0,0 +1,95 @@ +n, _ = list(map(int, input().split())) + +senquence_set = [] + +for i in range(n): + senquence_set.append(list(map(int, input().split()))) + +def is_heap(senquence): + max_or_min = senquence[0] - senquence[1] + + def deeper(start_of_layer, end_of_layer, senquence, max_or_min): + layer = senquence[start_of_layer:end_of_layer] + + if 2 * len(layer) > len(senquence[end_of_layer:]): + next_layer = senquence[end_of_layer:] + else: + next_layer = senquence[end_of_layer:end_of_layer + 2 * len(layer)] + + if not next_layer: return True + + i_of_node = 0 + + for node in layer: + left_of_node, right_of_node = i_of_node, i_of_node + 1 + + if i_of_node < len(next_layer): + if (node - next_layer[left_of_node]) * max_or_min < 0: + return False + if i_of_node + 1 < len(next_layer): + if (node - next_layer[right_of_node]) * max_or_min < 0: + return False + i_of_node += 2 + + return deeper(end_of_layer, end_of_layer + len(next_layer), senquence, max_or_min) + + return deeper(0,1,senquence,max_or_min), max_or_min, + +class tree: + def __init__(self, val): + self.val = val + self.left = None + self.right = None + +def get_Btree(array): + import math + depth = 1 + head = tree(array[0]) + layer = [head] + while layer: + k = 0 + tmp = [] + for node in layer: + left_index = int(math.pow(2, depth) - 1 + k) + if left_index < len(array): + new_node = tree(array[left_index]) + node.left = new_node + tmp.append(new_node) + if left_index + 1 < len(array): + new_node = tree(array[left_index + 1]) + node.right = new_node + tmp.append(new_node) + k += 2 + depth += 1 + layer = tmp + return head + +def postord(s, tree): + if tree.left: postord(s, tree.left) + if tree.right: postord(s, tree.right) + s.append(tree.val) + +def show_heap(senquence, is_heap_, max_or_min): + if not is_heap_: + print('Not Heap') + tree = get_Btree(senquence) + s = [] + postord(s, tree) + print(' '.join(list(map(str, s)))) + else: + if max_or_min > 0: + print('Max Heap') + tree = get_Btree(senquence) + s = [] + postord(s, tree) + print(' '.join(list(map(str, s)))) + else: + print('Min Heap') + tree = get_Btree(senquence) + s = [] + postord(s, tree) + print(' '.join(list(map(str, s)))) + +for senquence in senquence_set: + is_heap_, max_or_min = is_heap(senquence) + show_heap(senquence, is_heap_, max_or_min) diff --git a/AdvancedLevel_Python/1149.py b/AdvancedLevel_Python/1149.py new file mode 100644 index 0000000..099a838 --- /dev/null +++ b/AdvancedLevel_Python/1149.py @@ -0,0 +1,34 @@ +n, m = input().split() + +dangerous = {} + +for i in range(int(n)): + tmp = list(map(int, input().split())) + if not tmp[0] in dangerous.keys(): + dangerous[tmp[0]] = [tmp[1]] + else: + dangerous[tmp[0]].append([tmp[1]]) + if not tmp[1] in dangerous.keys(): + dangerous[tmp[1]] = [tmp[0]] + else: + dangerous[tmp[1]].append([tmp[0]]) + +ans = [] + +for i in range(int(m)): + tmp = list(map(int, input().split()))[1:] + is_safe = True + for good in tmp: + if good in dangerous.keys(): + for dangerous_make in dangerous[good]: + if dangerous_make in tmp: + is_safe = False + ans.append('No') + break + if not is_safe: + break + if is_safe: ans.append('Yes') + +[print(i) for i in ans] + + diff --git a/AdvancedLevel_Python/1150.py b/AdvancedLevel_Python/1150.py new file mode 100644 index 0000000..f9926a1 --- /dev/null +++ b/AdvancedLevel_Python/1150.py @@ -0,0 +1,57 @@ +n, m = list(map(int, input().split())) + +graph = [[101 for j in range(n)] for i in range(n)] + +for i in range(m): + tmp = list(map(int, input().split())) + graph[tmp[0] -1 ][tmp[1] - 1] = tmp[2] + graph[tmp[1] - 1][tmp[0] - 1] = tmp[2] + +k = int(input()) + +ans = [] + +possible = [] + +for j in range(k): + tmp = list(map(int, input().split())) + path = tmp[1:] + city_set = [i+ 1 for i in range(n)] + TotalDist = 0 + his = [] + is_simple = True + is_Ts_cycle = True + for i in range(len(path) - 1): + if TotalDist != 'NA' and graph[path[i] - 1][path[i + 1] -1 ] <= 100: + TotalDist += graph[path[i] - 1][path[i + 1] -1 ] + else: + TotalDist = 'NA' + + if path[i] in his: + is_simple = False + else: + his.append(path[i]) + + if path[0] != path[-1]: + is_Ts_cycle = False + his.append(path[0]) + + if set(his) != set(city_set): + is_Ts_cycle = False + + res = 'Path ' + str(j + 1) + ': ' + str(TotalDist) + ' ' + if is_Ts_cycle and TotalDist != 'NA': + possible.append([j+1, TotalDist]) + if is_simple: + res += '(TS simple cycle)' + else: + res += '(TS cycle)' + else: + res += '(Not a TS cycle)' + + ans.append(res) + +[print(i) for i in ans] +possible.sort(key=lambda x:x[1]) +print('Shortest Dist(' + str(possible[0][0]) +') = ' + str(possible[0][1]) ) + diff --git a/AdvancedLevel_Python/1151.py b/AdvancedLevel_Python/1151.py new file mode 100644 index 0000000..4716a6a --- /dev/null +++ b/AdvancedLevel_Python/1151.py @@ -0,0 +1,41 @@ +n, m =list(map(int, input().split())) + +inord = list(map(int, input().split())) + +preord = list(map(int, input().split())) + +tasks = [] + +for i in range(n): + tasks.append(list(map(int,input().split()))) + +def solve(preord, inord, task): + u, v = task[0], task[1] + root = preord[0] + left_inord, right_inord = inord[:inord.index(root)], inord[inord.index(root) + 1:] + left_preord, right_preord = preord[1:len(left_inord)+1], preord[len(left_inord) + 1:] + + if (u in preord and not v in preord): + return 'ERROR: ' + str(v) + ' is not found.' + elif (not u in preord and v in preord): + return 'ERROR: ' + str(u) + ' is not found.' + elif (not u in preord and not v in preord): + return 'ERROR: ' + str(u) + ' and ' + str(v) + ' are not found.' + + if u == root: + return str(u) + ' is an ancestor of ' + str(v) +'.' + if v == root: + return str(v) + ' is an ancestor of ' + str(u) + '.' + + elif (u in left_preord and v in right_preord) or (u in right_preord and v in left_preord): + return 'LCA of ' + str(u) + ' and ' + str(v) + ' is ' + str(root) + '.' + elif (u in left_preord and v in left_preord): + return solve(left_preord, left_inord, task) + elif (u in right_preord and v in right_preord): + return solve(right_preord, right_inord, task) + + +for task in tasks: + print(solve(preord, inord, task)) + + diff --git a/AdvancedLevel_Python/1152.py b/AdvancedLevel_Python/1152.py new file mode 100644 index 0000000..80e0e9e --- /dev/null +++ b/AdvancedLevel_Python/1152.py @@ -0,0 +1,20 @@ +n, k = list(map(int, input().split())) + +num = int(input()) + +from math import sqrt +def is_prime(num): + p = 2 + while p <= sqrt(num): + if num % p == 0: + return False + else: + p += 1 + return True + +for i in range(len(str(num)) - k + 1): + if is_prime(int(str(num)[i:i + k ])): + print(str(num)[i:i + k ]) + exit(0) + +print('404') diff --git a/AdvancedLevel_Python/1153.py b/AdvancedLevel_Python/1153.py new file mode 100644 index 0000000..dcfdb16 --- /dev/null +++ b/AdvancedLevel_Python/1153.py @@ -0,0 +1,52 @@ +n, m = list(map(int, input().split())) + +data = [] + +for i in range(n): + tmp = input() + cardnumber = tmp.split()[0] + scores = tmp.split()[1] + data.append({'cardnumber':tmp,'level':cardnumber[0], 'site':cardnumber[1:4], 'date':cardnumber[4:10], 'number':cardnumber[10:13], 'scores':scores}) + +for j in range(m): + tmp = input().split() + type = tmp[0] + term = tmp[1] + if type == '1': + print('Case ' + str(j+1) +': ' + type + ' ' + term) + ans = [] + for item in data: + if item['level'] == term: + ans.append(item) + ans.sort(key=lambda x:(-int(x['scores']), x['cardnumber'])) + if ans: [print(i['cardnumber']) for i in ans] + else: print('NA') + elif type == '2': + print('Case ' + str(j+1) + ': ' + type + ' ' + term) + ans = [] + for item in data: + if item['site'] == term: + ans.append(item) + from functools import reduce + p = 0 + for x in ans: + p += int(x['scores']) + if ans: print(str(len(ans)) +' ' + str(p)) + else: print('NA') + elif type == '3': + print('Case ' + str(j+1) + ': ' + type + ' ' + term) + ans = [] + for item in data: + if item['date'] == term: + ans.append(item) + ans_2 = [] + for e in ans: + ans_2.append(e['site']) + + from collections import Counter + c = Counter(ans_2) + c_ = sorted(c, key=lambda x:(-int(c[x]), int(x))) + + if not ans_2: print('NA') + else: [print(str(j) + ' ' + str(c[j])) for j in iter(c_)] + diff --git a/AdvancedLevel_Python/1154.py b/AdvancedLevel_Python/1154.py new file mode 100644 index 0000000..4c3b3a8 --- /dev/null +++ b/AdvancedLevel_Python/1154.py @@ -0,0 +1,24 @@ +n , m = list(map(int, input().split())) + +edges = [] + +for i in range(m): + edges.append(list(map(int, input().split()))) + +k = int(input()) + +tasks = [] + +def is_k_coloring(edges, tmp): + for edge in edges: + if tmp[edge[0]] == tmp[edge[1]]: + return False + return True + + +for i in range(k): + tmp = list(map(int, input().split())) + if is_k_coloring(edges, tmp): + print(str(len(set(tmp))) + '-coloring') + else: + print('No') diff --git a/AdvancedLevel_Python/1155.py b/AdvancedLevel_Python/1155.py new file mode 100644 index 0000000..559d16b --- /dev/null +++ b/AdvancedLevel_Python/1155.py @@ -0,0 +1,79 @@ +_ = input() + +arr = list(map(int, input().split())) + +class tree(): + def __init__(self, x): + self.val = x + self.left = None + self.right = None + self.touched = False + +def get_tree(arr): + head = tree(arr[0]) + layer = [head] + depth = 1 + from math import pow + while layer: + k = 0 + next_layer = [] + for node in layer: + left_index = int(pow(2, depth) - 1 + k) + if left_index < len(arr): + left_node = tree(arr[left_index]) + node.left = left_node + next_layer.append(left_node) + + if left_index + 1 < len(arr): + right_node = tree(arr[left_index + 1]) + node.right = right_node + next_layer.append(right_node) + + k += 2 + depth += 1 + layer = next_layer + return head + +def right_ord(head): + is_heap = True + if head.right: + max_min = head.val - head.right.val + if head.left: + max_min = head.val - head.left.val + + ans = [] + path = [head.val] + stack = [head] + while stack: + + last_node = stack[-1] + + if last_node.right and not last_node.right.touched: + path.append(last_node.right.val) + stack.append(last_node.right) + if (last_node.val - last_node.right.val) * (max_min) < 0: + is_heap = False + + elif last_node.left and not last_node.left.touched: + path.append(last_node.left.val) + stack.append(last_node.left) + if (last_node.val - last_node.left.val) * max_min < 0: + is_heap = False + else: + last_node.touched = True + stack.pop() + if not last_node.right and not last_node.left: + from copy import deepcopy + ans.append(deepcopy(path)) + path.pop() + return ans, is_heap, max_min + +ans, is_heap, max_min = right_ord(head=get_tree(arr)) + +[print(' '.join(list(map(str, path)))) for path in ans ] + +if is_heap: + max_min = 'Max' if max_min > 0 else 'Min' + print(max_min + ' Heap') +else: + print('Not Heap') diff --git a/AdvancedLevel_Python/README.md b/AdvancedLevel_Python/README.md new file mode 100644 index 0000000..e8f58e7 --- /dev/null +++ b/AdvancedLevel_Python/README.md @@ -0,0 +1,2 @@ +# PAT_PYTHON +try to be PYTHON MARS diff --git a/AdvancedLevel_Python/pat_exam/1.py b/AdvancedLevel_Python/pat_exam/1.py new file mode 100644 index 0000000..94951f3 --- /dev/null +++ b/AdvancedLevel_Python/pat_exam/1.py @@ -0,0 +1,26 @@ +n = input() + +import math + +def is_prime(x): + if x < 3: return x > 1 + + for i in range(2, int(math.sqrt(x)) + 1): + if x % i == 0: + return False + else: + return True + +all_prime = True +for i in range(len(n)): + tmp = n[i:] + if is_prime(int(tmp)): + print(tmp + ' ' + 'Yes') + else: + print(tmp + ' ' + 'No') + all_prime = False + +if all_prime: + print('All Prime!') + + diff --git a/AdvancedLevel_Python/pat_exam/2.py b/AdvancedLevel_Python/pat_exam/2.py new file mode 100644 index 0000000..71cd818 --- /dev/null +++ b/AdvancedLevel_Python/pat_exam/2.py @@ -0,0 +1,64 @@ + +def myread(): + return list(map(int, input().split())) + +a, b = myread() + +n, m = myread() + +data = [] + +rounds = [] + +for i in range(n): + data.append(myread()) + +rounds = [[data[j][i] for j in range(n)] for i in range(m)] + +his = {a:0} + +his[b] = 0 + +table = {abs(a - b):0} + + +def is_ok(his, table, new): + try: + a = table[new] + except: + return False + try: + b = his[new] + return False + except: + his[new] = 0 + + for num in his: + if num != new: + table[abs(num - new)] = 0 + + return True + +playing_player = [True for i in range(n)] + +report = [] + +for r in range(m): + for player_id in range(n): + if playing_player[player_id]: + new = rounds[r][player_id] + if is_ok(his, table, new): + pass + else: + playing_player[player_id] = False + report.append('Round #' + str(r + 1) +': ' + str(player_id + 1) + ' is out.') + +[print(message) for message in report] + +winners = [id + 1 for id in range(n) if playing_player[id]] + +if not winners: + print('No winner.') +else: + print('Winner(s): ' + ' ' .join(list(map(str, winners)))) + diff --git a/AdvancedLevel_Python/pat_exam/3.py b/AdvancedLevel_Python/pat_exam/3.py new file mode 100644 index 0000000..80af9ae --- /dev/null +++ b/AdvancedLevel_Python/pat_exam/3.py @@ -0,0 +1,32 @@ +n, r , k = list(map(int, input().split())) + +edges = [] + +for i in range(r): + tmp = list(map(int, input().split())) + edges.append(tmp) + +m = int(input()) + +quert_list = [] + +for i in range(m): + quert_list.append(list(map(int, input().split()))) + +def judge(edges, query, k): + if len(list(set(query))) < k: + return 'Error: Too few species.' + elif len(list(set(query))) > k: + return 'Error: Too many species.' + + for edge in edges: + if query[edge[0] - 1] == query[edge[1] - 1]: + return 'No' + else: + return 'Yes' + +[print(judge(edges, query, k)) for query in quert_list] + + + + diff --git a/AdvancedLevel_Python/pat_exam/4.py b/AdvancedLevel_Python/pat_exam/4.py new file mode 100644 index 0000000..3ea7690 --- /dev/null +++ b/AdvancedLevel_Python/pat_exam/4.py @@ -0,0 +1,37 @@ +n, m = list(map(int, input().split())) + +arr = list(map(int, input().split())) + +runs = {i:[] for i in range(n)} + +memory = [arr.pop(0) for i in range(m)] + +guide = {v:0 for v in memory} + +k = 0 + +while memory: + tmp = [v for v in memory if guide[v] == k] + if not tmp: + k += 1 + tmp = [v for v in memory if guide[v] == k] + + now_index = memory.index(min(tmp)) + now = memory.pop(now_index) + runs[k].append(now) + + if arr: + new = arr.pop(0) + if new >= now: + guide[new] = k + else: + guide[new] = k + 1 + memory.append(new) + +[print(' '.join(list(map(str, run)))) for run in runs.values() if run] + + +""" +13 3 +81 94 11 96 12 99 2 2 2 17 35 28 58 41 75 15 10 9 8 7 6 5 4 +""" \ No newline at end of file diff --git a/PTA_MOOC_Python/README.md b/PTA_MOOC_Python/README.md new file mode 100644 index 0000000..246aa22 --- /dev/null +++ b/PTA_MOOC_Python/README.md @@ -0,0 +1,80 @@ +# PTA_ZJU_mooc +PTA of ZJU_mooc (浙大数据结构moocPTA平台习题 )\ +by PIGLAKE\ +in PYTHON + +PTA:https://pintia.cn/problem-sets/1211841066264109056 + +#### QUESTION LIST: + 1_01-复杂度1 最大子列和问题\ + 2_01-复杂度2 Maximum Subsequence Sum\ + 3_02-线性结构2 一元多项式的乘法与加法运算\ + 4_02-线性结构3 Reversing Linked List\ + 5_02-线性结构4 Pop Sequence\ + 6_03-树1 树的同构\ + 7_03-树2 List Leaves\ + 8_03-树3 Tree Traversals Again\ + 9_04-树4 是否同一棵二叉搜索树\ + 10_04-树5 Root of AVL Tree\ + 11_04-树6 Complete Binary Search Tree\ + 12_05-树7 堆中的路径\ + 13_05-树8 File Transfer\ + 14_05-树9 Huffman Codes\ + 15_06-图1 列出连通集\ + 16_06-图2 Saving James Bond - Easy Version\ + 17_06-图3 六度空间\ + 18_07-图4 哈利·波特的考试\ + 19_07-图5 Saving James Bond - Hard Version\ + 20_07-图6 旅游规划\ + 21_08-图7 公路村村通\ + 22_08-图8 How Long Does It Take\ + 23_08-图9 关键活动\ + 24_09-排序1 排序\ + 25_09-排序2 Insert or Merge\ + 26_09-排序3 Insertion or Heap Sort\ + 27_10-排序4 统计工龄\ + 28_10-排序5 PAT Judge\ + 29_10-排序6 Sort with Swap(0, i)\ + 30_11-散列1 电话聊天狂人\ + 31_11-散列2 Hashing\ + 32_11-散列3 QQ帐户的申请与登陆\ + 33_11-散列4 Hashing - Hard Version\ + 34_KMP 串的模式匹配\ + + +#### Details: + 1.O(n)遍历\ + 2.在1的基础上修改即可\ + 3.字典实现加,遍历列表实现乘,字典合并\ + 4.转list反转。有极限\ + 5.用两个栈实现\ + 6.建树逐层遍历\ + 7.逐层遍历\ + 8.==(先序+中序)求后序,递归求解\ + 9.分为左右子树递归\ + 10.偷懒(直接用了老师给的c代码)\ + 11.找根节点,二分递归\ + 12.调库\ + 13.线性list实现并查集,查找优化,有极限\ + 14.计算WPL,查前缀码(不建树),有极限\ + 15.list存图,递归DFS,BFS,(注意python list(set(会变序,算是bug(被折磨了一时)\ + 16.随便写就行\ + 17.BFS遍历即可,有极限\ + 18.三重循环的Floyd算法,有极限\ + 19.简单版的代码改了改,每次deep都check min_start_jump_distance条件\ + 20.(ill today 被折磨了一时)改一下dijsk算法,有极限\ + 21.最小生成树-简单的贪心dsk(无向图),有极限\ + 22.有向无环图\ + 23.22略改(由于明天过生日下午摸鱼了会导致写的很难看\ + 24.调库\ + 25.非递归的Merge\ + 26.堆与插入\ + 27.调库\ + 28.排!就硬排!(有极限)\ + 29.Python是没有极限的!\ + 30.字典\ + 31.素数+散列表+quadratic probing\ + 32.字典\ + 33.easy\ + 34.python超时,后用c重写\ + diff --git a/PTA_MOOC_Python/amp.py b/PTA_MOOC_Python/amp.py new file mode 100644 index 0000000..66a2c55 --- /dev/null +++ b/PTA_MOOC_Python/amp.py @@ -0,0 +1,28 @@ +def getNext(t): + j, i = -1, 0 + next = [-1]*len(t) + while i < len(t)-1: + if -1 == j or t[i] == t[j]: + i, j = i + 1, j + 1 + next[i] = j + else: + j = next[j] + return next + +def KMP(s,t): + next = getNext(t) + j, i = -1, -1 + while j != len(t) and i < len(s): + if s[i] == t[j] or j == -1: + i, j = i + 1, j + 1 + else: + j = next[j] + return s[i-j:] if j == len(t) else "Not Found" + +s = input() +n = int(input()) +data = [] +for i in range(n): + data.append(input()) + +[print(KMP(s,i)) for i in data] diff --git a/PTA_MOOC_Python/src01.py b/PTA_MOOC_Python/src01.py new file mode 100644 index 0000000..7840964 --- /dev/null +++ b/PTA_MOOC_Python/src01.py @@ -0,0 +1,20 @@ +data = [] +for i in range(2): + tmp = input() + data.append(tmp) + +if not data[1]:exit(0) + +array = list(map(int, data[1].split(' '))) + +s, tmp = 0, 0 + +for i in array: + tmp += i + if tmp > s: + s = tmp + elif tmp < 0: + tmp = 0 + +print(max(s,0)) + diff --git a/PTA_MOOC_Python/src02.py b/PTA_MOOC_Python/src02.py new file mode 100644 index 0000000..20ecb2b --- /dev/null +++ b/PTA_MOOC_Python/src02.py @@ -0,0 +1,40 @@ +data = [] +for i in range(2): + tmp = input() + data.append(tmp) + +if not data[1]:exit(0) + +array = list(map(int, data[1].split(' '))) + +array = array[::-1] + +ans, tmp_arr = [], [] + +s, tmp = 0, 0 + +if max(array) < 0: + print(0, array[-1], array[0]) + exit(0) + +elif max(array) == 0: + print(0, 0, 0) + exit(0) + + +for i in array: + tmp += i + tmp_arr.append(i) + if tmp >= s: + s = tmp + ans = tmp_arr[:] + elif tmp < 0: + tmp = 0 + tmp_arr = [] + + +while ans[0] == 0 or ans[-1] == 0: + if ans[0] == 0:ans.pop(0) + else:ans.pop() + +print(s, ans[-1], ans[0]) diff --git a/PTA_MOOC_Python/src03.py b/PTA_MOOC_Python/src03.py new file mode 100644 index 0000000..a954249 --- /dev/null +++ b/PTA_MOOC_Python/src03.py @@ -0,0 +1,83 @@ +a = input() +b = input() + +def data_process(a): + ans = [] + d = dict() + a = a.split() + del a[0] + for i in range(int(len(a) / 2)): + ans.append([a[2 * i], a[2 * i + 1]]) + d[str(a[2 * i + 1])] = a[2 * i] + return ans, d + + +ans1, d1 = data_process(a) + +ans2, d2 = data_process(b) + + +def add(d1, d2): + x = list(set([i for i in d1.keys()] + [i for i in d2.keys()])) + d_new = dict() + + for i in x: + d_new[i] = 0 + if i in d1.keys(): + d_new[i] += int(d1[i]) + + if i in d2.keys(): + d_new[i] += int(d2[i]) + + return d_new + +def mult(d1, d2): + ans = [] + for i in d1: + for j in d2: + ans.append([str(int(i[1]) + int(j[1])), str(int(i[0]) * int(j[0]))]) + dictory = dict() + for e in ans: + try: + dictory[e[0]] += int(e[1]) + except: + dictory[e[0]] = int(e[1]) + return dictory + +res1, res2 = mult(ans1, ans2), add(d1, d2) + + +keys1 = list(map(int, list(res1.keys()))) +keys1.sort(reverse=True) +out1 = '' +for k in keys1: + if res1[str(k)] == 0: + pass + else: + out1 += str(res1[str(k)]) + out1 += ' ' + out1 += str(k) + out1 += ' ' +out1 = out1[:len(out1) -1] + +if not out1: + print('0 0') +else: + print(out1) +keys2 = list(map(int, list(res2.keys()))) +keys2.sort(reverse=True) +out2 = '' +for k in keys2: + if res2[str(k)] == 0: + pass + else: + out2 += str(res2[str(k)]) + out2 += ' ' + out2 += str(k) + out2 += ' ' +out2 = out2[:len(out2) -1] +if not out2: + print('0 0') +else: + print(out2) + diff --git a/PTA_MOOC_Python/src04.py b/PTA_MOOC_Python/src04.py new file mode 100644 index 0000000..129c8dd --- /dev/null +++ b/PTA_MOOC_Python/src04.py @@ -0,0 +1,31 @@ +[address0, length, k] = input().split() +length, k = int(length), int(k) +mass = {} + +for i in range(length): + data = input().split() + mass[data[0]] = [data[1], data[2]] + +address = mass[address0][1] +array = [[address0] + mass[address0]] + +while address != '-1' : + array.append([address] + mass[address]) + address = mass[address][1] + +k_ = int(len(array) / k) +index = 0 +ans = [] +for i in range(k_): + ans += [j for j in array[index:index + k][::-1]] + index += k + +ans += array[k * k_:] + +for i in range(len(ans) - 1): + ans[i][2] = ans[i+1][0] + +ans[-1][2] = '-1' + +for e in ans: + print(' '.join(e)) diff --git a/PTA_MOOC_Python/src05.py b/PTA_MOOC_Python/src05.py new file mode 100644 index 0000000..75327ff --- /dev/null +++ b/PTA_MOOC_Python/src05.py @@ -0,0 +1,35 @@ +[depth, n, k] = list(map(int, input().split())) + +data = [] + +for i in range(k): + data.append(list(map(int, input().split()))) + +def interop(nums, tmp): + tmp.append(nums.pop()) + +for i in data: + tmp = [] + nums = [o+1 for o in range(n)][::-1] + process = True + for e in i: + while nums or tmp: + #print(nums, tmp, e) + if not tmp:interop(nums, tmp) + if tmp[-1] == e: + tmp.pop() + break + else: + if not nums: + print('NO') + process = False + break + elif len(tmp) >= depth: + print('NO') + process = False + break + else: + interop(nums, tmp) + if not process:break + if process:print('YES') + diff --git a/PTA_MOOC_Python/src06.py b/PTA_MOOC_Python/src06.py new file mode 100644 index 0000000..2ca2cdb --- /dev/null +++ b/PTA_MOOC_Python/src06.py @@ -0,0 +1,77 @@ +def get_tree(): + nums = int(input()) + node = dict() + for i in range(nums): + node[str(i)] = input().split() + return nums, node + +def get_root(nums, tree): + tmp = [] + for i in tree.values(): + tmp += i[1:] + for i in range(nums): + if not str(i) in tmp: + return tree[str(i)] + +nums1, tree1 = get_tree() +nums2, tree2 = get_tree() + +#print(tree1) +#print(tree2) + +if not tree1 or not tree2: + if not tree1 and not tree2: + print('Yes') + exit(0) + else: + print('No') + exit(0) + +root1, root2 = get_root(nums1, tree1), get_root(nums2, tree2) + +#print(root1, root2) + +from collections import Counter + +def judge(n1, n2, tree1, tree2): + if n1[0] != n2[0]: + #print(n1[0], n2[0]) + print('No') + exit(0) + if Counter(n1)['-'] != Counter(n2)['-']: + #print(Counter(n1)['-'], Counter(n2)['-']) + print('No') + exit(0) + if not n1[0]: + return 0 + else: + l1, r1 = deeper(n1, tree1) + if not n2[0]: + return 0 + else: + l2, r2 = deeper(n2, tree2) + + if l1[0] == l2[0] and r1[0] == r2[0]: + judge(l1, l2, tree1, tree2) + judge(r1, r2, tree1, tree2) + elif l1[0] == r2[0] and r1[0] == l2[0]: + judge(l1, r2, tree1, tree2) + judge(r1, l2, tree1, tree2) + else: + print('No') + exit(0) + +def deeper(node, tree): + if not node[1] == '-': + left = tree[node[1]] + else: + left = [None] + if not node[2] == '-': + right = tree[node[2]] + else: + right = [None] + return left,right + +judge(root1, root2, tree1, tree2) +print('Yes') + diff --git a/PTA_MOOC_Python/src07.py b/PTA_MOOC_Python/src07.py new file mode 100644 index 0000000..3d73967 --- /dev/null +++ b/PTA_MOOC_Python/src07.py @@ -0,0 +1,50 @@ +def get_tree(): + nums = int(input()) + node = dict() + for i in range(nums): + node[str(i)] = [str(i)] + input().split() + return nums, node + +def get_root(nums, tree): + tmp = [] + for i in tree.values(): + tmp += i[1:] + for i in range(nums): + if not str(i) in tmp: + return tree[str(i)] + +nums, tree = get_tree() +root = get_root(nums, tree) + +#print(tree) +#print(root) + +def deeper(node, tree): + if not node[0] == '-': + left = tree[node[0]] + else: + left = [None] + if not node[1] == '-': + right = tree[node[1]] + else: + right = [None] + return left,right + +from collections import Counter + +def dive(root, tree): + ans = '' + layer = [root] + while layer: + layer_new = [] + for node in layer: + if Counter(node)['-'] == 2: + ans += node[0] + ans += ' ' + for i in range(1,3): + if node[i] != '-': + layer_new.append(tree[node[i]]) + layer = layer_new + print(ans[:-1]) +dive(root, tree) + diff --git a/PTA_MOOC_Python/src08.py b/PTA_MOOC_Python/src08.py new file mode 100644 index 0000000..eb9d121 --- /dev/null +++ b/PTA_MOOC_Python/src08.py @@ -0,0 +1,26 @@ +n = int(input()) +oper = [] +for i in range(2 * n): + oper.append(input().split()) +#preli = [(i + 1) for i in range(n)] +inli = [] +tmp = [] +preli = [] +for i in oper: + if i[0] == 'Push': + preli.append(int(i[1])) + tmp.append(int(i[1])) + if i[0] == 'Pop': + inli.append(tmp.pop()) + +def cycle(preli, inli): + if not preli: + return [] + if len(preli) == 1: + return preli + key = inli.index(preli[0]) + return cycle(preli[1:key+1], inli[:key]) + cycle(preli[key+1:], inli[key+1:]) + [preli[0]] + +ans = cycle(preli, inli) +c = list(map(str, ans)) +print(' '.join(c)) diff --git a/PTA_MOOC_Python/src08_.py b/PTA_MOOC_Python/src08_.py new file mode 100644 index 0000000..69a6087 --- /dev/null +++ b/PTA_MOOC_Python/src08_.py @@ -0,0 +1,44 @@ +n = int(input()) + +f = [] +temp = [] +m = [] +l = [] +for i in range(2 * n): + x = input().split() + if x[0] == 'Push': + f.append(int(x[1])) + temp.append(int(x[1])) + else: + m.append(temp.pop()) +del temp + +if n == 1: + print(str(m[0])) + exit(0) + +def reConstruction (after,pre,cen): + if len(pre)==0: + return + if len(pre)==1: + return (pre[0]) + after.append(pre[0]) + i=0 + for i in range (len(cen)): + if cen[i]==pre[0]: + break + left_cen=cen[:i] + right_cen=cen[i+1:] + left_pre = pre[1:i+1] + right_pre = pre[i+1:] + + after.append(reConstruction(after, right_pre, right_cen)) + after.append(reConstruction (after,left_pre,left_cen)) + +reConstruction (l,f,m) +res=[] +for i in l: + if i !=None: + res.append(str(i)) +print(res) +print(' '.join(res[::-1])) diff --git a/PTA_MOOC_Python/src09.py b/PTA_MOOC_Python/src09.py new file mode 100644 index 0000000..30d8c0d --- /dev/null +++ b/PTA_MOOC_Python/src09.py @@ -0,0 +1,25 @@ +n = input().split() +data = [] +ans = [] +while len(n) != 1: + case = [] + for i in range(int(n[1])+1): + case.append(list(map(int, input().split()))) + data.append(case) + n = input().split() + +def judge(std, test): + if not std and not test:return True + if std[0] != test[0]:return False + return judge([i for i in std[1:] if i < std[0]], [i for i in test[1:] if i < test[0]])\ + and judge([i for i in std[1:] if i > std[0]] , [i for i in test[1:] if i > std[0]]) + +for case in data: + for i in range(len(case)): + if not i == 0: + if judge(case[0], case[i]): + ans.append('Yes') + else: + ans.append('No') +for result in ans: + print(result) \ No newline at end of file diff --git a/PTA_MOOC_Python/src10.py b/PTA_MOOC_Python/src10.py new file mode 100644 index 0000000..32babe9 --- /dev/null +++ b/PTA_MOOC_Python/src10.py @@ -0,0 +1,59 @@ +n = int(input()) +data = input().split() +i = 0 +code = dict() +while i + 1 <= len(data): + code[data[i]] = int(data[i+1]) + i += 2 +k = int(input()) +test_list = [] + +for i in range(k): + tmp_dict = dict() + for j in range(n): + tmp = input().split() + tmp_dict[tmp[0]] = tmp[1] + test_list.append(tmp_dict) + +def get_WPL(code, test=None): + WPL = 0 + if not test: + ans = [] + tmp = 0 + nums = list(code.values()) + nums.sort(reverse=True) + while nums: + tmp = 0 + tmp += nums.pop() + if not nums: + break + tmp += nums.pop() + nums.append(tmp) + nums.sort(reverse=True) + ans.append(tmp) + return sum(ans) + for key in test.keys(): + WPL += len(test[key]) * code[key] + return WPL + +def is_conflict(case): + tmp = list(case.values()) + tmp.sort(key=lambda x:len(x)) + #print(tmp) + for i in range(len(tmp) - 1): + for j in range(i+1, len(tmp)): + #print(tmp[i], tmp[j][:len(tmp[i])]) + if tmp[i] == tmp[j][:len(tmp[i])]: + + return False + return True + +ground_truth = get_WPL(code) +for case in test_list: + if get_WPL(code, case) != ground_truth: + print('No') + continue + if not is_conflict(case): + print('No') + continue + print('Yes') \ No newline at end of file diff --git a/PTA_MOOC_Python/src11.py b/PTA_MOOC_Python/src11.py new file mode 100644 index 0000000..e3ab896 --- /dev/null +++ b/PTA_MOOC_Python/src11.py @@ -0,0 +1,38 @@ +n = int(input()) +nums = list(map(int, input().split())) +nums.sort() + +class tree: + def __init__(self, val): + self.val = val + self.left = None + self.right = None + +def get_root(n): + from math import log, pow + c = int(log(n+1,2) - 1) + k = pow(2, c+1) - 1 + return int(pow(2, c) - 1 + (((n - k) - pow(2, c)) if (n - k) > pow(2, c) else 0)) + +def get_Btree(array): + if not array:return None + if len(array) == 1: + return tree(array[0]) + root = (get_root(len(array))) + head = tree(array[-(root + 1)]) + head.left = get_Btree(array[:-(root+1)]) + head.right = get_Btree(array[-(root):]) if not root == 0 else [] + return head + +tree = get_Btree(nums) +layer = [tree] +ans = [] +while layer: + layer_tmp = [] + for node in layer: + ans.append(node.val) + if node.left:layer_tmp.append(node.left) + if node.right:layer_tmp.append(node.right) + layer = layer_tmp + +print(' '.join(list(map(str, ans)))) \ No newline at end of file diff --git a/PTA_MOOC_Python/src12.py b/PTA_MOOC_Python/src12.py new file mode 100644 index 0000000..8a893b6 --- /dev/null +++ b/PTA_MOOC_Python/src12.py @@ -0,0 +1,20 @@ +[n, k] = list(map(int, input().split())) + +nums = list(map(int, input().split())) + +index = list(map(int, input().split())) + +from heapq import heappush + +heap = [] + +for n in nums: + heappush(heap, n) + +for i in index: + tmp = [] + j = i + while j >= 1: + tmp.append(str(heap[j-1])) + j = int(j/2) + print(' '.join(tmp)) diff --git a/PTA_MOOC_Python/src13.py b/PTA_MOOC_Python/src13.py new file mode 100644 index 0000000..7f1bc32 --- /dev/null +++ b/PTA_MOOC_Python/src13.py @@ -0,0 +1,54 @@ +n = int(input()) +array = [-1 for i in range(n)] +oper = [] +tmp = input().split() +while tmp[0] != 'S' : + oper.append(tmp) + tmp = input().split() + +def is_connected(oper1, oper2, array): + if Counter(array)[-1] == 0: + return 'yes' + father1 = oper1 + while array[father1] != -1: + father1 = array[father1] + array[oper1] = father1 if father1 != oper1 else -1 + + father2 = oper2 + while array[father2] != -1: + father2 = array[father2] + array[oper2] = father2 if father2 != oper2 else -1 + #print(array) + if father1 == father2: + return 'yes' + else: + return 'no' + +from collections import Counter +for inst in oper: + if inst[0] == 'C': + print(is_connected(int(inst[1]) - 1, int(inst[2]) - 1, array)) + else: + #print(array) + tmp = min(int(inst[1]), int(inst[2])) - 1 + num_of_None = Counter(array)[-1] + if int(inst[1]) > int(inst[2]): + father = int(inst[1]) - 1 + if num_of_None == 0: + break + while array[father] != -1: + father = array[father] + array[father] = tmp + else: + if num_of_None == 0: + break + father = int(inst[2]) - 1 + while array[father] != -1: + father = array[father] + array[father] = tmp +#print(array) +k = Counter(array)[-1] +if k == 1: + print('The network is connected.') +else: + print('There are '+ str(k) + ' components.') diff --git a/PTA_MOOC_Python/src14.py b/PTA_MOOC_Python/src14.py new file mode 100644 index 0000000..32babe9 --- /dev/null +++ b/PTA_MOOC_Python/src14.py @@ -0,0 +1,59 @@ +n = int(input()) +data = input().split() +i = 0 +code = dict() +while i + 1 <= len(data): + code[data[i]] = int(data[i+1]) + i += 2 +k = int(input()) +test_list = [] + +for i in range(k): + tmp_dict = dict() + for j in range(n): + tmp = input().split() + tmp_dict[tmp[0]] = tmp[1] + test_list.append(tmp_dict) + +def get_WPL(code, test=None): + WPL = 0 + if not test: + ans = [] + tmp = 0 + nums = list(code.values()) + nums.sort(reverse=True) + while nums: + tmp = 0 + tmp += nums.pop() + if not nums: + break + tmp += nums.pop() + nums.append(tmp) + nums.sort(reverse=True) + ans.append(tmp) + return sum(ans) + for key in test.keys(): + WPL += len(test[key]) * code[key] + return WPL + +def is_conflict(case): + tmp = list(case.values()) + tmp.sort(key=lambda x:len(x)) + #print(tmp) + for i in range(len(tmp) - 1): + for j in range(i+1, len(tmp)): + #print(tmp[i], tmp[j][:len(tmp[i])]) + if tmp[i] == tmp[j][:len(tmp[i])]: + + return False + return True + +ground_truth = get_WPL(code) +for case in test_list: + if get_WPL(code, case) != ground_truth: + print('No') + continue + if not is_conflict(case): + print('No') + continue + print('Yes') \ No newline at end of file diff --git a/PTA_MOOC_Python/src15.py b/PTA_MOOC_Python/src15.py new file mode 100644 index 0000000..1d08be9 --- /dev/null +++ b/PTA_MOOC_Python/src15.py @@ -0,0 +1,57 @@ +length, n = input().split() +data = [] +for i in range(int(n)): + data.append(list(map(int, input().split()))) +G = [[] for i in range(int(length))] + +for edge in data: + G[int(edge[0])].append(int(edge[1])) + G[int(edge[1])].append(int(edge[0])) + +for d in G: d.sort() + +def DFS(start, ans, G): + if not G[start] in ans: ans.append(start) + else: return ans + for i in G[start]: + if not i in ans: + ans = DFS(i, ans, G) + return ans + +def BFS(layer, ans, G): + if not layer: return ans + layer_ = [] + for node in layer: + ans.append(node) + for node in layer: + for i in G[node]: + if not i in ans: + if not i in layer_: layer_.append(i) + ans = BFS(layer_, ans, G) + return ans + +out1, out2 = [], [] + +for i in range(len(G)): + index1 = True + index2 = True + G[i].sort() + for subset in out1: + if i in subset: + index1 = False + if index1: + tmp1 = DFS(i, [], G) + out1.append(tmp1) + for s in out2: + if i in s: + index2 = False + if index2: + tmp2 = BFS([i], [], G) + out2.append(tmp2) + +left_ = '{ ' +right_ = ' }' +for i in out1: + print(left_ + ' '.join(list(map(str, i))) + right_) +for j in out2: + print(left_ + ' '.join(list(map(str, j))) + right_) diff --git a/PTA_MOOC_Python/src16.py b/PTA_MOOC_Python/src16.py new file mode 100644 index 0000000..25c1261 --- /dev/null +++ b/PTA_MOOC_Python/src16.py @@ -0,0 +1,60 @@ +def read(): + return list(map(int, input().split())) +global d +n, d = read() +G = [] +for i in range(int(n)): + G.append(read()) + +from math import sqrt +def get_distance(a, b): + return sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) + +def is_safe(vector): + if vector[0] + d >= 50 or vector[0] - d <= -50 or vector[1] + d >= 50 or vector[1] - d <= -50: + return True + else: return False + +def BFS(layer, his, G): + if not layer: return + his += layer + layer_ = [] + for node in layer: + for p in range(len(G)): + if not p in his: + if get_distance(G[node], G[p]) <= d: + layer_.append(p) + return layer_, his + +def get_ans(G): + ans = [] + for i in range(len(G)): + if is_safe(G[i]): + ans.append(i) + return ans + +def get_layer(G): + layer = [] + for i in range(len(G)): + if get_distance([0,0], G[i]) <= d + 7.5: + layer.append(i) + return layer + +ans = get_ans(G) +layer = get_layer(G) + +for p in layer: + if p in ans: + print('Yes') + exit(0) +if not ans or not layer: + print('No') + exit(0) +his = [layer[0]] +while layer: + layer, his = BFS(layer, his, G) + for p in layer: + if p in ans: + print('Yes') + exit(0) +print('No') diff --git a/PTA_MOOC_Python/src17.py b/PTA_MOOC_Python/src17.py new file mode 100644 index 0000000..bddd517 --- /dev/null +++ b/PTA_MOOC_Python/src17.py @@ -0,0 +1,34 @@ +length, n = input().split() +data = [] +for i in range(int(n)): + data.append(list(map(int, input().split()))) +G = [[] for i in range(int(length))] + +for edge in data: + G[int(edge[0])-1].append(int(edge[1])-1) + G[int(edge[1])-1].append(int(edge[0])-1) + +for d in G: d.sort() + +def BFS(layer, ans, G, k): + #print(layer, ans, k) + if k > 6:return ans + if not layer: return ans + layer_ = [] + for node in layer: + ans.append(node) + for node in layer: + for i in G[node]: + if not i in ans: + if not i in layer_: layer_.append(i) + k += 1 + ans = BFS(layer_, ans, G, k) + return ans + +out = [] +for i in range(len(G)): + tmp = BFS([i], [], G, 0) + out.append(len(tmp) / int(length)) + +for i in range(len(out)): + print(str(i+1)+ ': '+'%.2f'%(out[i] * 100)+ '%') diff --git a/PTA_MOOC_Python/src18.py b/PTA_MOOC_Python/src18.py new file mode 100644 index 0000000..83baa8e --- /dev/null +++ b/PTA_MOOC_Python/src18.py @@ -0,0 +1,31 @@ +length, n = list(map(int, input().split())) +edge = [] + +for i in range(n): + edge.append(list(map(int, input().split()))) +top = 100 * length +1 +G = [[top for i in range(length)] for i in range(length)] + +for e in edge: + G[e[0]-1][e[1]-1] = e[2] + G[e[1] - 1][e[0] - 1] = e[2] + +for i in range(length): + for j in range(length): + for k in range(length): + if i != j: + if G[i][j] > G[i][k] + G[k][j]: + G[i][j] = G[i][k] + G[k][j] + +ans = [] + +for i in range(len(G)): + tmp = [j for j in G[i] if j != top] + if not tmp: pass + elif len(tmp) == length - 1: + ans.append(max(tmp)) + +if not ans: print(0);exit(0) +c = min(ans) +index = ans.index(c) +print(index+1, c) \ No newline at end of file diff --git a/PTA_MOOC_Python/src19.py b/PTA_MOOC_Python/src19.py new file mode 100644 index 0000000..128e21a --- /dev/null +++ b/PTA_MOOC_Python/src19.py @@ -0,0 +1,78 @@ +def read(): + return list(map(int, input().split())) + +global d +n, d = read() +G = [] +for i in range(int(n)): + G.append(read()) + +from math import sqrt + +def get_distance(a, b): return sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) + +def is_safe(vector): + if vector[0] + d >= 50 or vector[0] - d <= -50 or vector[1] + d >= 50 or vector[1] - d <= -50: + return True + else: return False + +def BFS(layer, his, path, G): + if not layer: return + his += layer + layer_ = [] + path_ = {} + for node in layer: + for p in range(len(G)): + if not p in his: + if get_distance(G[node], G[p]) <= d: + layer_.append(p) + if not str(p) in path_.keys(): + path_[str(p)] = path[str(node)] + [p] + else: + pre = G[path_[str(p)][0]] + post = G[node] + if get_distance([0,0], post) < get_distance([0,0], pre): + path_[str(p)] = path[str(node)] + [p] + return layer_, his, path_ + +def get_ans(G): + ans = [] + for i in range(len(G)): + if is_safe(G[i]): + ans.append(i) + return ans + +def get_layer(G): + layer = [] + for i in range(len(G)): + if get_distance([0,0], G[i]) <= d + 7.5: + layer.append(i) + return layer + +ans, layer = get_ans(G), get_layer(G) + +for p in layer: + if p in ans:print(1);exit(0) + +if not ans or not layer:print(0);exit(0) + +his, path = [], {str(i):[i] for i in layer} +cycle = True + +while layer and cycle: + layer, his, path = BFS(layer, his, path, G) + result = [] + for p in layer: + if p in ans: + tmp = [G[i] for i in path[str(p)]] + result.append(tmp) + cycle = False + +if not result:print(0);exit(0) + +print(len(result[0]) + 1) + +result.sort(key = lambda x:get_distance([0,0],x[0])) + +for i in result[0]: + print(' '.join(list(map(str, i)))) \ No newline at end of file diff --git a/PTA_MOOC_Python/src20.py b/PTA_MOOC_Python/src20.py new file mode 100644 index 0000000..7f8c366 --- /dev/null +++ b/PTA_MOOC_Python/src20.py @@ -0,0 +1,30 @@ +n, m, s, d = list(map(int, input().split())) +edge = [] +for i in range(m):edge.append(list(map(int, input().split()))) +global TOP +TOP = n*500+1 +G, G_ = [[TOP for j in range(n)]for i in range(n)],[[n*500+1 for j in range(n)]for i in range(n)] + +for e in edge: + G[e[0]][e[1]], G[e[1]][e[0]] = e[2], e[2] + G_[e[0]][e[1]], G_[e[1]][e[0]] = e[3], e[3] + +def Dsk(s,d,G): + dis = [i for i in G[s]] + cost = [j for j in G_[s]] + path = [s] + while not len(path) == n: + index = dis.index(min([i for i in dis if not i in path])) + path.append(index) + for i in range(len(dis)): + if i != s and not i in path: + if G[index][s] + G[index][i] < dis[i]: + dis[i] = G[index][s] + G[index][i] + cost[i] = G_[index][s] + G_[index][i] + elif G[index][s] + G[index][i] == dis[i] and cost[i] > G_[index][s] + G_[index][i]: + cost[i] = G_[index][s] + G_[index][i] + + return dis[d],cost[d] + +shortcut,c = Dsk(s,d,G) +print(shortcut, c) \ No newline at end of file diff --git a/PTA_MOOC_Python/src21.py b/PTA_MOOC_Python/src21.py new file mode 100644 index 0000000..a0f9256 --- /dev/null +++ b/PTA_MOOC_Python/src21.py @@ -0,0 +1,23 @@ +def read(): + return list(map(int, input().split())) + +n, k = read() +if k < n - 1:print(-1);exit(0) +G = [[63353 for j in range(n)] for i in range(n)] +for i in range(k): + data = read() + G[data[0]-1][data[1]-1] = data[2] + G[data[1]-1][data[0]-1] = data[2] + +def Dsk(s,G): + path = [s] + ans = 0 + while not len(path) == n: + room = [min([[G[node][i],i] for i in range(len(G[node])) if not i in path]) for node in path] + room.sort(key = lambda x:x[0]) + path.append(room[0][1]) + ans += room[0][0] + return ans + +res = Dsk(s=0,G=G) +print(-1 if res > 63353 else res) diff --git a/PTA_MOOC_Python/src22.py b/PTA_MOOC_Python/src22.py new file mode 100644 index 0000000..ea162ab --- /dev/null +++ b/PTA_MOOC_Python/src22.py @@ -0,0 +1,51 @@ +def read(): + return list(map(int, input().split())) +global n +n, k = read() +G = [[63353 for j in range(n)] for i in range(n)] +data = {} + +for i in range(k): + input_ = read() + if not input_[0] in data.keys(): + data[input_[0]] = [input_[1:]] + else: + data[input_[0]].append(input_[1:]) + G[input_[0]][input_[1]] = input_[2] + +def get_degree(node_list, data): + degree = [] + for node in node_list: + tmp = 0 + for edges in data.values(): + for edge in edges: + if edge[0] == node: + tmp -= 1 + degree.append(tmp) + return degree + +node_list = [i for i in range(n)] +his = [] +layer_pre = [] +time_schedule = {} +while len(his) < n: + degree = get_degree(node_list, data) + layer_rn = [i for i in range(len(degree)) if degree[i] == 0 and (not i in his)] + if not layer_rn: print('Impossible');exit(0) + tmp = [] + if layer_pre: + for son in layer_rn: + val_son = [] + for node in layer_pre: + if G[node][son] != 63353: + val_son.append(G[node][son] + time_schedule[node]) + time_schedule[son] = max(val_son) + else: + for son in layer_rn: + time_schedule[son] = 0 + his += layer_rn + for i in layer_rn: + if i in data.keys(): del data[i] + layer_pre = layer_rn + +print(max(time_schedule.values())) diff --git a/PTA_MOOC_Python/src23.py b/PTA_MOOC_Python/src23.py new file mode 100644 index 0000000..f1b7de2 --- /dev/null +++ b/PTA_MOOC_Python/src23.py @@ -0,0 +1,110 @@ +def read(): + return list(map(int, input().split())) +global n +n, k = read() +global Top +Top = 633533444 + +G = [[Top for j in range(n)] for i in range(n)] +data = {} +atab = {} +table = [] +for i in range(k): + input_ = read() + table.append(input_) + if not input_[0]-1 in data.keys(): + data[input_[0]-1] = [[input_[1]-1, input_[2]]] + else: + data[input_[0]-1].append([input_[1]-1, input_[2]]) + + if not input_[1]-1 in atab.keys(): + atab[input_[1]-1] = [[input_[0]-1, input_[2]]] + else: + atab[input_[1]-1].append([input_[0]-1, input_[2]]) + + G[input_[0]-1][input_[1]-1] = input_[2] + +def get_degree(node_list, data): + degree = [] + for node in node_list: + tmp = 0 + for edges in data.values(): + for edge in edges: + if edge[0] == node: + tmp -= 1 + degree.append(tmp) + return degree + +node_list = [i for i in range(n)] +his = [] +layer_pre = [] +time_schedule = {} +while len(his) < n: + degree = get_degree(node_list, data) + layer_rn = [i for i in range(len(degree)) if degree[i] == 0 and (not i in his)] + if not layer_rn: print(0);exit(0) + tmp = [] + if layer_pre: + for son in layer_rn: + val_son = {} + for node in layer_pre: + if G[node][son] != Top: + val_son[str(node+1) + '->' + str(son+1)] = G[node][son] + time_schedule[node] + time_schedule[son] = max(val_son.values()) + else: + for son in layer_rn: + time_schedule[son] = 0 + his += layer_rn + for i in layer_rn: + if i in data.keys(): del data[i] + layer_pre = layer_rn + +min_time = max(time_schedule.values()) + +ddl_schedule = {} + +def get_degree_T(node_list, atab): + degree = [] + for node in node_list: + tmp = 0 + for edges in atab.values(): + for edge in edges: + if edge[0] == node: + tmp -= 1 + degree.append(tmp) + return degree + +his_T = [] +layer_post = [] + +while len(his_T) < n: + degree = get_degree_T(node_list, atab) + layer_rn = [i for i in range(len(degree)) if degree[i] == 0 and (not i in his_T)] + if not layer_rn: print(0);exit(0) + tmp = [] + if layer_post: + for son in layer_rn: + val_son = {} + for node in layer_post: + if G[son][node] != Top: + val_son[str(node+1) + '->' + str(son+1)] = ddl_schedule[node] - G[son][node] + ddl_schedule[son] = min(val_son.values()) + else: + for son in layer_rn: + ddl_schedule[son] = min_time + his_T += layer_rn + for i in layer_rn: + if i in atab.keys(): del atab[i] + layer_post = layer_rn + +output = [] + +for edge in table[::-1]: + if time_schedule[edge[0]-1] + G[edge[0]-1][edge[1]-1] == ddl_schedule[edge[1]-1]: + output.append(str(edge[0])+'->'+str(edge[1])) + +output.sort(key=lambda x:int(x.split('-')[0])) +#print(output) +print(min_time) + +[print(o) for o in output] diff --git a/PTA_MOOC_Python/src24.py b/PTA_MOOC_Python/src24.py new file mode 100644 index 0000000..c21ce0f --- /dev/null +++ b/PTA_MOOC_Python/src24.py @@ -0,0 +1,4 @@ +n = input() +array = list(map(int, input().split())) +array.sort() +print(' '.join(list(map(str, array)))) diff --git a/PTA_MOOC_Python/src25.py b/PTA_MOOC_Python/src25.py new file mode 100644 index 0000000..ca98163 --- /dev/null +++ b/PTA_MOOC_Python/src25.py @@ -0,0 +1,65 @@ +Merge = False +Insertion = False +def merge(seq, low, mid, high): + left = seq[low: mid] + right = seq[mid: high] + k = 0 + j = 0 + result = [] + while k < len(left) and j < len(right): + if left[k] <= right[j]: + result.append(left[k]) + k += 1 + else: + result.append(right[j]) + j += 1 + result += left[k:] + result += right[j:] + seq[low: high] = result + +def Sort(arr, arr2, indict, Merge, Insertion): + FUCKYOU = 1 + for i in range(1, len(arr)): + key = arr[i] + j = i - 1 + while j >= 0 and key < arr[j]: + arr[j + 1] = arr[j] + j -= 1 + arr[j + 1] = key + + if FUCKYOU < len(arr2): + low = 0 + while low < len(arr2): + mid = low + FUCKYOU # mid前后均为有序 + high = min(low + 2 * FUCKYOU, len(arr2)) + if mid < high: + merge(arr2, low, mid, high) + low += 2 * FUCKYOU + FUCKYOU *= 2 + + #print(arr) + #print(arr2) + + if Merge or Insertion: + if Merge: + print('Merge Sort') + print(' '.join(list(map(str, arr2)))) + exit(0) + elif Insertion: + print('Insertion Sort') + print(' '.join(list(map(str, arr)))) + exit(0) + else: + if arr == indict: + Insertion = True + if arr2 == indict: + Merge = True + +n = int(input()) +arr = list(map(int, input().split())) +arr2 = arr.copy() +indict = list(map(int, input().split())) +seq = arr.copy() + + +Sort(arr, arr2, indict,Merge,Insertion) diff --git a/PTA_MOOC_Python/src26.py b/PTA_MOOC_Python/src26.py new file mode 100644 index 0000000..b86ea84 --- /dev/null +++ b/PTA_MOOC_Python/src26.py @@ -0,0 +1,62 @@ +Heap = False +Insertion = False + +def heapify(arr, n, i): + largest = i + l = 2 * i + 1 # left = 2*i + 1 + r = 2 * i + 2 # right = 2*i + 2 + + if l < n and arr[i] < arr[l]: + largest = l + + if r < n and arr[largest] < arr[r]: + largest = r + + if largest != i: + arr[i], arr[largest] = arr[largest], arr[i] # 交换 + + heapify(arr, n, largest) + +def Sort(arr, arr2, indict, Heap, Insertion): + n = len(arr2) + for i in range(n, -1, -1): + heapify(arr2, n, i) + FUCK = n - 1 + for i in range(1, len(arr)): + key = arr[i] + j = i - 1 + while j >= 0 and key < arr[j]: + arr[j + 1] = arr[j] + j -= 1 + arr[j + 1] = key + + if FUCK >= 0: + arr2[FUCK], arr2[0] = arr2[0], arr2[FUCK] # 交换 + heapify(arr2, FUCK, 0) + FUCK -= 1 + #print(arr) + #print(arr2) + + if Heap or Insertion: + if Heap: + print('Heap Sort') + print(' '.join(list(map(str, arr2)))) + exit(0) + elif Insertion: + print('Insertion Sort') + print(' '.join(list(map(str, arr)))) + exit(0) + else: + if arr == indict: + Insertion = True + if arr2 == indict: + Heap = True + +n = int(input()) +arr = list(map(int, input().split())) +arr2 = arr.copy() +indict = list(map(int, input().split())) +seq = arr.copy() + +Sort(arr, arr2, indict,Heap,Insertion) + diff --git a/PTA_MOOC_Python/src27.py b/PTA_MOOC_Python/src27.py new file mode 100644 index 0000000..368a77a --- /dev/null +++ b/PTA_MOOC_Python/src27.py @@ -0,0 +1,10 @@ +n = int(input()) +array = list(map(int, input().split())) + +from collections import Counter +c = Counter(array) + +keys = list(c.keys()) +keys.sort() + +[print(str(i)+':'+str(c[i])) for i in keys] diff --git a/PTA_MOOC_Python/src28.py b/PTA_MOOC_Python/src28.py new file mode 100644 index 0000000..9a9a554 --- /dev/null +++ b/PTA_MOOC_Python/src28.py @@ -0,0 +1,54 @@ +m, k, n = list(map(int, input().split())) +s = list(map(int, input().split())) + +data = {} +Compile = {} +for i in range(int(n)): + new_input = input().split() + if new_input[0] in data.keys(): + if data[new_input[0]][int(new_input[1])-1] == '-': + data[new_input[0]][int(new_input[1]) - 1] = max(int(new_input[2]),0) + if int(new_input[2]) != -1: + Compile[new_input[0]] = True + else: + data[new_input[0]][int(new_input[1])-1] = max(int(new_input[2]), data[new_input[0]][int(new_input[1])-1]) + + else: + data[new_input[0]] = ['-' for i in range(k)] + data[new_input[0]][int(new_input[1]) - 1] = max(int(new_input[2]),0) + Compile[new_input[0]] = False + if int(new_input[2]) != -1: + Compile[new_input[0]] = True + +score = {} +for key in data.keys(): + score[key] = sum([i for i in data[key] if i != '-']) + +distribution = list(set(score.values())) +d = list(score.values()) +d.sort(reverse=True) +rank = {} +for key in distribution: + rank[key] = d.index(key) + 1 + +key_list = list(score.keys()) +key_list.sort(key=lambda x:score[x],reverse=True) + +score_extra = {} +for key in key_list: + score_extra[key] = sum([1 for i in range(len(data[key])) if data[key][i] == s[i]]) + +for i in range(len(key_list)): + for j in range(i+1, len(key_list)): + if score[key_list[i]] == score[key_list[j]]: + if score_extra[key_list[j]] == score_extra[key_list[i]]: + if int(key_list[j]) < int(key_list[i]): + key_list[i], key_list[j] = key_list[j], key_list[i] + elif score_extra[key_list[j]] > score_extra[key_list[i]]: + key_list[i], key_list[j] = key_list[j], key_list[i] + else: + pass + +for i in key_list: + if Compile[i]: + print(str(rank[score[i]]) + ' ' + i + ' ' +str(score[i]) + ' ' + ' '.join(list(map(str, data[i])))) diff --git a/PTA_MOOC_Python/src29.py b/PTA_MOOC_Python/src29.py new file mode 100644 index 0000000..62470e4 --- /dev/null +++ b/PTA_MOOC_Python/src29.py @@ -0,0 +1,22 @@ +#coperate with vohyz ,Python! Forever God! +l = input() +arr = list(map(int, input().split())) + +k, b = 0, 0 +for i in range(len(arr)): + if arr[i]>0: + if arr[i] != i: + l = 2 + tmp = arr[i] + while arr[tmp] != i and arr[tmp] != -1: + index = tmp + tmp = arr[tmp] + arr[index] = -1 + l += 1 + arr[tmp] = -1 + k += 1 + b += l +if arr[0] == 0: + print(b+k) +else: + print(b+k-2) diff --git a/PTA_MOOC_Python/src30.py b/PTA_MOOC_Python/src30.py new file mode 100644 index 0000000..aa2d43c --- /dev/null +++ b/PTA_MOOC_Python/src30.py @@ -0,0 +1,24 @@ +n = int(input()) + +monsry = {} + +for i in range(n): + tmp = input().split() + if tmp[0] in monsry.keys(): + monsry[tmp[0]][0] += 1 + else: + monsry[tmp[0]] = [1,tmp[0]] + if tmp[1] in monsry.keys(): + monsry[tmp[1]][0] += 1 + else: + monsry[tmp[1]] = [1, tmp[1]] + +top = max(monsry.values(), key = lambda x:x[0]) + +c = list(map(int, [p[1] for p in monsry.values() if p[0] == top[0]])) +res = str(min(c)) + ' ' + str(top[0]) + +if len(c) > 1: + print(res + ' ' + str(len(c))) +else: + print(res) \ No newline at end of file diff --git a/PTA_MOOC_Python/src31.py b/PTA_MOOC_Python/src31.py new file mode 100644 index 0000000..057546e --- /dev/null +++ b/PTA_MOOC_Python/src31.py @@ -0,0 +1,45 @@ +m, _ = list(map(int, input().split())) + +seq = list(map(int, input().split())) + +prime = 0 +if m == 1: prime = 2; +else: + import math + while True: + f=True + a=2 + while a<=int(math.sqrt(m)): + if m%a==0: + f=False + break + a+=1 + if f: + prime = m + break + m += 1 + +array = {} +res = [] + +for i in seq: + key = i % prime + if key not in array.keys(): + array[key] = i + res.append(key) + else: + a = 1 + over = False + j = 0 + while j <= prime: + j += 1 + if (key + a) % prime not in array.keys(): + array[(key + a)%prime] = i + res.append(int((key + a) % prime)) + over = True + break + if not over: + a = (math.sqrt(a) + 1) ** 2 + if not over: res.append('-') +from functools import reduce +print(reduce(lambda x,y:str(x)+' '+str(y), res)) \ No newline at end of file diff --git a/PTA_MOOC_Python/src32.py b/PTA_MOOC_Python/src32.py new file mode 100644 index 0000000..8042267 --- /dev/null +++ b/PTA_MOOC_Python/src32.py @@ -0,0 +1,20 @@ +n = int(input()) +data = {} +res = [] +for i in range(n): + tmp = input().split() + if tmp[1] in data.keys(): + if tmp[0] == 'N': + res.append('ERROR: Exist') + if tmp[0] == 'L': + if tmp[2] == data[tmp[1]][2]: + res.append('Login: OK') + else: + res.append('ERROR: Wrong PW') + elif tmp[0] == 'N': + data[tmp[1]] = tmp + res.append('New: OK') + else: + res.append('ERROR: Not Exist') + +[print(i) for i in res] \ No newline at end of file diff --git a/PTA_MOOC_Python/src33.py b/PTA_MOOC_Python/src33.py new file mode 100644 index 0000000..40490a4 --- /dev/null +++ b/PTA_MOOC_Python/src33.py @@ -0,0 +1,28 @@ +prime = int(input()) + +seq = list(map(int, input().split())) + +m = [list(map(lambda x:x % prime if x!= -1 else -1,[x]))[0] for x in seq] + +seq_ = sorted([[i, seq[i]] for i in range(len(seq)) if seq[i] > 0], key=lambda x:x[1]) + +mo = [0 for i in range(len(m))] +ans = [] +p = 0 +while len(ans) != len(seq_): + add_of_p = seq_[p][0] + value_of_p = seq_[p][1] + designed_add = m[add_of_p] + if add_of_p == designed_add and mo[add_of_p] == 0: + mo[add_of_p] = 1 + ans.append(value_of_p) + p = 0 + elif 0 not in mo[designed_add if designed_add != len(seq) -1 else 0:add_of_p] and mo[add_of_p] == 0: + mo[add_of_p] = 1 + ans.append(value_of_p) + p = 0 + else: + p += 1 + +from functools import reduce +print(reduce(lambda x,y:str(x)+' '+str(y), ans)) \ No newline at end of file