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

Envío 771

Problema 0xcf - Mirando al horizonte

  • Autor: abatesins
  • Fecha: 2020-09-15 19:34:13 UTC (Hace más de 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.027 s 3 KBi
#2
Correcto
0.042 s 5 KBi
#3
Correcto
0.039 s 7 KBi
#4
Correcto
0.036 s 3 KBi
#5
Correcto
0.041 s 5 KBi
#6
Tiempo límite excedido
0.659 s 39 KBi
#7
Tiempo límite excedido
0.694 s 23 KBi
#8
Tiempo límite excedido
1.081 s 89 KBi
#9
Tiempo límite excedido
0.766 s 18 KBi
#10
Tiempo límite excedido
1.015 s 77 KBi
Puntos totales: 50 / 100

Código

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


def adopt(parent, child):
    parent.child = child
    child.parent = parent


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


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)))