Cod sursa(job #2750198)

Utilizator Andrei_SturzuAndrei Sturzu Andrei_Sturzu Data 10 mai 2021 10:45:50
Problema Zeap Scor 10
Compilator py Status done
Runda Arhiva de probleme Marime 2.14 kb
import heapq

file = 'zeap.in'

with open(file, 'rt') as f:
    content = f.readlines()
    content = [line.strip().split() for line in content]
    ops = content
    for op in ops:
        if len(op) > 1:
            op[1] = int(op[1])
    # print(content)

zeap = []
inserted = {}

with open("zeap.out", 'a') as f:

    for op in ops:

        if op[0] == "I":
            try:
                if inserted[op[1]] == 0:
                    heapq.heappush(zeap, op[1])
                    inserted[op[1]] = 1
                    # print(zeap)
            except KeyError:
                heapq.heappush(zeap, op[1])
                inserted[op[1]] = 1
                # print(zeap)

        elif op[0] == "S":
            try:
                i = zeap.index(op[1])
                zeap[i] = zeap[-1]
                zeap.pop()
                heapq.heapify(zeap)
            except ValueError:
                # with open("zeap.out", 'a') as f:
                f.write("-1\n")
                # print(-1)

        elif op[0] == "C":
            try:
                res = zeap.index(op[1])
                # print(1)
                # with open("zeap.out", 'a') as f:
                f.write("1\n")
            except ValueError:
                # with open("zeap.out", 'a') as f:
                f.write("0\n")
                # print(0)

        elif op[0] == "MAX":
            if len(zeap) < 2:
                # with open("zeap.out", 'a') as f:
                f.write("-1\n")
            else:
                # with open("zeap.out", 'a') as f:
                f.write(str(abs(zeap[0] - heapq.nlargest(1, zeap)[0])))
                f.write("\n")
            # print(abs(zeap[0] - heapq.nlargest(1, zeap)[0]))

        elif op[0] == "MIN":
            if len(zeap) < 2:
                # with open("zeap.out", 'a') as f:
                f.write("-1\n")
            else:
                min_dif = heapq.nsmallest(2, zeap)
                # with open("zeap.out", 'a') as f:
                f.write(str(abs(min_dif[1] - min_dif[0])))
                f.write("\n")
            # print(abs(min_dif[1] - min_dif[0]))