Cod sursa(job #3129466)

Utilizator bobic.teona20Bobic Teona-Christiana bobic.teona20 Data 14 mai 2023 19:05:44
Problema Zeap Scor 0
Compilator py Status done
Runda Arhiva de probleme Marime 1.61 kb
import heapq

Z = set()
Q = []

def numar(S):
    return int(S[:-1])

def Insert(x):
    global Z
    if x not in Z:
        Z.add(x)
        if len(Z) >= 2:
            it = Z.index(x)
            if it != 0:
                Q.append((abs(x - Z[it-1]), (Z[it-1], x)))
            if it != len(Z) - 1:
                Q.append((abs(Z[it+1] - x), (Z[it+1], x)))

def Delete(x):
    global Z, Q
    if x not in Z:
        print("-1")
    else:
        it = Z.index(x)
        right = it + 1
        if it == 0 or right == len(Z):
            Z.remove(x)
        else:
            left = it - 1
            if right < len(Z) and it > 0:
                Q.append((abs(Z[right] - Z[left]), (Z[left], Z[right])))
            Z.remove(x)

def Search(x):
    global Z
    print(1 if x in Z else 0)

def MIN():
    global Z, Q
    if len(Z) < 2:
        print("-1")
    else:
        while Q and (Q[0][1][0] not in Z or Q[0][1][1] not in Z):
            heapq.heappop(Q)
        if Q:
            print(Q[0][0])
        else:
            print("-1")

def MAX():
    global Z
    if len(Z) < 2:
        print("-1")
    else:
        print(Z[-1] - Z[0])

with open("zeap.out", "w") as out:
    with open("zeap.in", "r") as f:
        for line in f:
            S = line.strip()
            if S[0] == 'I':
                Insert(numar(S[2:]))
            elif S[0] == 'S':
                Delete(numar(S[2:]))
            elif S[0] == 'C':
                Search(numar(S[2:]))
            elif S[0] == 'M' and S[1] == 'I':
                MIN()
            else:
                MAX()