目录
★实验任务
给你一个集合,一开始是个空集,有如下两种操作:
向集合中插入一个元素。
询问集合中最接近某个数的数是多少。
★数据输入
输入第一行为一个正整数N,表示共有N个操作。
接下来N行,每行一个操作。
对于第一个操作,输入格式为1 x,表示往集合里插入一个值为x的元素。
对于第二个操作,输入格式为2 x,表示询问集合中最接近x的元素是什么。
1<=N<=100000,1<=x<=1000000000。
★数据输出
对于所有的第二个操作,输出一个或者两个整数,表示最接近x的元素,有两个数的情况,按照升序输出,并用一个空格隔开。
如果集合为空,输出一行“Empty!”
数据保证插入的元素两两不同。
★代码实现
- def find(i:int):
- if not my_set:
- print('Empty!')
- return
- if i in my_set:
- print(i)
- return
- for k in my_set:
- my_list.append(abs(i-k))
- k=min(my_list)
- if (i-k) in my_set:
- print(i-k,end=' ')
- if (i+k) in my_set:
- print(i+k)
-
- my_set=set()
- num=int (input())
- while num:
- num-=1
- my_list=[]
- cont = [int(n) for n in (input().split())]
- if cont[0]==1:
- my_set.add(cont[1])
- else:
- find(cont[1])
★实验任务
给你一颗空的 AVL 树,你需要完成两个操作:
向树中插入一个元素。
询问某个元素到根的路径。
PS:在任意时刻,这样的 AVL 树是唯一的。
★数据输入
输入第一行为一个正整数 N,表示共有 N 个操作。
接下来 N 行,每行一个操作。
对于第一个操作,输入格式为 1 x,表示往集合里插入一个值为 x 的元素。
对于第二个操作,输入格式为 2 x,表示询问根到 x 这个元素的路径。
1<=N<=100000,1<=x<=1000000000。
★数据输出
对于所有的第二个操作,输出一行若干个整数,表示根到 x 的路径序列,数与数之间用空格隔开。
题目保证每个数最多只会插入一次,并且保证询问的数在之前已经插入过。
输入示例 |
---|
5 1 2 1 1 1 3 2 1 2 3 |
输出示例 |
---|
2 1 2 3 |
★代码实现
- class Node(object):
- def __init__(self, val):
- self.val = val
- self.lchild = None
- self.rchild = None
-
- class tn():
- def __init__(self):
- self.num=0
-
- class TreeNode():
- def height(self, root):
- if root == None:
- return 0
- else:
- return root.height
-
- def LL(self, root):
- node = root.lchild
- root.lchild = node.rchild
- node.rchild = root
- root.height = max(self.height(root.lchild), self.height(root.rchild)) + 1
- node.height = max(self.height(node.lchild), self.height(root.rchild)) + 1
- return node
-
- def RR(self, root):
- node = root.rchild
- root.rchild = node.lchild
- node.lchild = root
- root.height = max(self.height(root.rchild), self.height(root.lchild)) + 1
- node.height = max(self.height(node.lchild), self.height(root.rchild)) + 1
- return node
-
- def LR(self, root):
- root.lchild = self.RR(root.lchild)
- return self.LL(root)
-
- def RL(self, root):
- root.rchild = self.LL(root.rchild)
- return self.RR(root)
-
- def insert(self, root, val):
- if root == None:
- root = Node(val)
- else:
- if val < root.val:
- root.lchild = self.insert(root.lchild, val)
- if abs(self.height(root.lchild) - self.height(root.rchild)) > 1:
- if val < root.lchild.val:
- root = self.LL(root)
- else:
- root = self.LR(root)
- else:
- root.rchild = self.insert(root.rchild, val)
- if abs(self.height(root.lchild) - self.height(root.rchild)) > 1:
- if val > root.rchild.val:
- root = self.RR(root)
- else:
- root = self.RL(root)
- root.height = max(self.height(root.lchild), self.height(root.rchild)) + 1
- return root
-
- # 查询
- def find(self, root, val):
- if root.val == val:
- if T.num:
- print("", end=' ')
- print(root.val, end='')
- T.num -= 1
- return True
- else:
- if val < root.val:
- if T.num:
- print("",end=' ')
- print(root.val,end='')
- T.num-=1
- return self.find(root.lchild, val)
- else:
- if T.num:
- print("", end=' ')
- print(root.val, end='')
- T.num -= 1
- return self.find(root.rchild, val)
-
- T=tn()
- t = TreeNode()
- root = None
- num=int (input())
- while num:
- num-=1
- cont = [int(n) for n in (input().split())]
- if cont[0]==1:
- root = t.insert(root,cont[1])
- else:
- T.num=0
- t.find(root,cont[1])