Task: | Ruudukko |
Sender: | okkokko |
Submission time: | 2022-11-09 19:29:46 +0200 |
Language: | Python3 (PyPy3) |
Status: | READY |
Result: | 0 |
group | verdict | score |
---|---|---|
#1 | TIME LIMIT EXCEEDED | 0 |
#2 | TIME LIMIT EXCEEDED | 0 |
#3 | TIME LIMIT EXCEEDED | 0 |
test | verdict | time | group | |
---|---|---|---|---|
#1 | TIME LIMIT EXCEEDED | -- | 1, 2, 3 | details |
#2 | TIME LIMIT EXCEEDED | -- | 1, 2, 3 | details |
#3 | TIME LIMIT EXCEEDED | -- | 1, 2, 3 | details |
#4 | TIME LIMIT EXCEEDED | -- | 2, 3 | details |
#5 | TIME LIMIT EXCEEDED | -- | 2, 3 | details |
#6 | TIME LIMIT EXCEEDED | -- | 2, 3 | details |
#7 | TIME LIMIT EXCEEDED | -- | 3 | details |
#8 | TIME LIMIT EXCEEDED | -- | 3 | details |
#9 | TIME LIMIT EXCEEDED | -- | 3 | details |
Code
# completes tests 1 and 2, but not 3import randomimport timeimport sysfrom typing import Generator, TypeVardef I():return map(int, input().split())DIVISOR = 10**9 + 7_V = TypeVar("_V")test_N = 1000test_expected_results = {100: 773224951,200: 452330452,300: 37161558,400: 250332178,1000: 298053617}def test():n = test_Ndef text_gen():yield str(n)random.seed(1001)for i in range(n):yield " ".join((str(random.randint(1, n * n)) for _ in range(n)))return text_gen().__next__input = test()class Node:"Binary Search Tree node"def __init__(self, parent: "Node|None", left: "Node|None", right: "Node|None", key: "int|float"):self.key = keyself.parent = parentself.left = leftself.right = rightself.values = []self.marker = 0passdef Get(self) -> "list":return self.values...def Add(self, value):self.values.append(value)def next(self):if self.marker == 0:self.marker += 1if self.left:return self.leftif self.marker == 1:self.marker += 1if self.right:return self.rightself.marker = 0return self.parentclass BST:def __init__(self, middle: float):self.root = Node(None, None, None, middle)def Insert(self, key: "int | float", value: "_V"):node, pos = self.search(key)if pos == 1:node.left = Node(node, None, None, key)node.left.Add(value)elif pos == 2:node.right = Node(node, None, None, key)node.right.Add(value)else:node.Add(value)# def Get(self, key: "int | float") -> "Node":# ...def Iter(self) -> "Generator[Node,None,None]":node = self.rootwhile 1:new = node.next()if new is None:breakif node.marker == 0:# new is parentif new.marker == 1:# node is leftyield newelif new.left is None:yield new# elif node.marker == 1:# # new is right childnode = newdef Iter_Values(self):# for node in self.Iter():# for x, y in node.Get():# yield node.key, x, yreturn ((node.key, v) for node in self.Iter() for v in node.Get())def search(self, key: "int|float") -> "tuple[Node, int]":"""returns a node and a number.0 means the node has the wanted value,1 means the value doesn't exist but would be the node's left branch if inserted,2 means the same as 1 but for the right branch."""# no recursioncurrent = self.rootwhile 1:if key < current.key:if current.left:current = current.leftelse:return current, 1elif key > current.key:if current.right:current = current.rightelse:return current, 2else:return current, 0def main():timer_0 = time.perf_counter()n, = I()grid = [list(I()) for _ in range(n)]total = 0timer_1 = time.perf_counter()print("timer 1:", timer_1 - timer_0, file=sys.stderr)line_row = [0] * nline_row_last = [1] * nline_col = [0] * nline_col_last = [1] * nline_row_added = [0] * nline_col_added = [0] * n# exists_row = [set(g) for g in grid]# exists_all = set().union(*exists_row)# print("exists_all size:", len(exists_all))# values_coords = BST(n * n // 2)sett = set()for y in range(n):for x in range(n):# values_coords.Insert(grid[y][x], (x, y))sett.add((grid[y][x], x, y))def get_line_values_row(y: int, v: int):"""tells how many routes can start from a tile of value v and first go along row y \nsum of the route values of tiles less than v on row y"""if v == line_row_last[y]: # false <==> first time with this y and vreturn line_row[y]line_row_last[y] = vs = line_row[y] + line_row_added[y]line_row_added[y] = 0line_row[y] = sreturn sdef get_line_values_col(x: int, v: int):if v == line_col_last[x]:return line_col[x]line_col_last[x] = vs = line_col[x] + line_col_added[x]line_col_added[x] = 0line_col[x] = sreturn sdef calculate_routes(y: int, x: int, number: int):if number == 1:value = 1else:value = (get_line_values_col(x, number) + get_line_values_row(y, number) + 1) % DIVISOR# nonlocal total# total += valueline_row_added[y] += valueline_col_added[x] += valuereturn valuetimer_2 = time.perf_counter()print("timer 2:", timer_2 - timer_1, file=sys.stderr)sor = sorted(sett)timer_3 = time.perf_counter()print("timer 3:", timer_3 - timer_2, file=sys.stderr)total = sum(calculate_routes(y, x, k)for k, x, y in sor)timer_4 = time.perf_counter()print("timer 4:", timer_4 - timer_3, file=sys.stderr)return total % DIVISORdef old_iteration(n, grid, exists_row, exists_all, calculate_routes):for a in range(1, n * n + 1):if a not in exists_all:continuefor y in range(n):if a not in exists_row[y]:continuex = -1try:while True:x = grid[y].index(a, x + 1)calculate_routes(y, x, a)except ValueError:passtimer_start = time.perf_counter()w = main()print(w)# 100: 773224951# 200: 452330452print("time:", time.perf_counter() - timer_start,("\ncorrect" if w == test_expected_results[test_N] else "\ndifferent") if test_N in test_expected_results.keys() else "no data", file=sys.stderr)
Test details
Test 1
Group: 1, 2, 3
Verdict: TIME LIMIT EXCEEDED
input |
---|
3 1 1 1 1 1 1 1 1 1 |
correct output |
---|
9 |
user output |
---|
(empty) |
Test 2
Group: 1, 2, 3
Verdict: TIME LIMIT EXCEEDED
input |
---|
3 1 2 3 6 5 4 7 8 9 |
correct output |
---|
135 |
user output |
---|
(empty) |
Test 3
Group: 1, 2, 3
Verdict: TIME LIMIT EXCEEDED
input |
---|
3 7 8 1 4 5 4 3 9 6 |
correct output |
---|
57 |
user output |
---|
(empty) |
Test 4
Group: 2, 3
Verdict: TIME LIMIT EXCEEDED
input |
---|
100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
correct output |
---|
10000 |
user output |
---|
(empty) |
Test 5
Group: 2, 3
Verdict: TIME LIMIT EXCEEDED
input |
---|
100 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
correct output |
---|
187458477 |
user output |
---|
(empty) |
Test 6
Group: 2, 3
Verdict: TIME LIMIT EXCEEDED
input |
---|
100 2995 8734 1018 2513 7971 5063 ... |
correct output |
---|
964692694 |
user output |
---|
(empty) |
Test 7
Group: 3
Verdict: TIME LIMIT EXCEEDED
input |
---|
1000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
correct output |
---|
1000000 |
user output |
---|
(empty) |
Test 8
Group: 3
Verdict: TIME LIMIT EXCEEDED
input |
---|
1000 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
correct output |
---|
229147081 |
user output |
---|
(empty) |
Test 9
Group: 3
Verdict: TIME LIMIT EXCEEDED
input |
---|
1000 520283 805991 492643 75254 527... |
correct output |
---|
951147313 |
user output |
---|
(empty) |