█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 764

Problema 0xcf - Mirando al horizonte

  • Autor: abatesins
  • Fecha: 2020-09-15 13:17:49 UTC (Hace más de 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.026 s 3 KBi
#2
Correcto
0.022 s 3 KBi
#3
Correcto
0.031 s 7 KBi
#4
Correcto
0.029 s 3 KBi
#5
Correcto
0.037 s 3 KBi
#6
Tiempo límite excedido
0.548 s 38 KBi
#7
Tiempo límite excedido
0.981 s 70 KBi
#8
Tiempo límite excedido
0.604 s 52 KBi
#9
Tiempo límite excedido
0.572 s 36 KBi
#10
Tiempo límite excedido
1.048 s 76 KBi
Puntos totales: 50 / 100

Código

class Node(object):
    def __init__(self, value, idx):
        self.parent = None
        self.children = []
        self.value = value  # height
        self.idx = idx  # in the original array

    def is_root(self):
        return self.parent is None

    def __lt__(self, other):
        return self.value < other.value

def case(n_arr, arr):
    tree = []
    view_heights = []
    
    for idx, val in enumerate(arr[::-1]):
        idx = n_arr - idx - 1
        node = Node(val, idx)
        if len(tree) == 0:
            view_heights.append(-1)
            tree.append(node)
            continue
        else:
            # search parent
            last_node = tree[-1]
            while True:
                if node < last_node:
                    # add as child of last_node
                    node.parent = last_node
                    last_node.children.append(node)
                    view_heights.append(last_node.value)
                    tree.append(node)
                    break
                else:
                    # go up the tree
                    if last_node.is_root():
                        # add node as new root
                        last_node.parent = node
                        node.children.append(last_node)
                        view_heights.append(-1)
                        tree.append(node)
                        break
                    else:
                        last_node = last_node.parent
    return view_heights[::-1]

if __name__ == "__main__":
    num_cases = int(input())
    for case_nr in range(1, num_cases+1):
        n_arr = int(input())
        arr = list(map(int, input().split(' ')))
        view_heights = case(n_arr, arr)
        print(f'Case #{case_nr}: ' + ' '.join(map(str, view_heights)))