你看这个代码对吗?import heapq
class Flyer:
def __init__(self, name, flyer_id, time, status):
self.name = name
self.id = flyer_id
self.time = time
self.status = status
self.cancelled = False
def __str__(self):
return "({}, {}, {}, {})".format(self.name, self.id, self.time, self.status)
def __repr__(self):
return self.__str__()
def __lt__(self, other):
# For max heap, we reverse the comparison
status_rank = {"Super": 4, "Platinum": 3, "Gold": 2, "Silver": 1}
self_rank = status_rank[self.status]
other_rank = status_rank[other.status]
if self_rank != other_rank:
return self_rank > other_rank # Higher status has higher priority
else:
return self.time < other.time # Earlier time has higher priority
class NUAUpgradeSystem:
def __init__(self):
self.heap = []
self.flyer_map = {}
def add_flyer(self, name, flyer_id, time, status):
"""Add a flyer to the system"""
flyer = Flyer(name, flyer_id, time, status)
self.flyer_map[flyer_id] = flyer
heapq.heappush(self.heap, flyer)
def cancel_flyer(self, flyer_id):
"""Cancel a flyer's upgrade request"""
if flyer_id in self.flyer_map:
self.flyer_map[flyer_id].cancelled = True
def get_top_k(self, k):
"""Get k highest priority non-cancelled flyers using lazy deletion"""
result = []
temp_heap = []
# Use lazy deletion: extract until we find k valid flyers
while len(result) < k and self.heap:
current = heapq.heappop(self.heap)
if not current.cancelled:
result.append(current)
# Push to temp heap to restore later
temp_heap.append(current)
# Restore the original heap
for flyer in temp_heap:
heapq.heappush(self.heap, flyer)
return result
def process_input(self, input_lines):
"""Process input lines and return results"""
if not input_lines:
return []
# Parse first line
first_line = input_lines[0].split()
if len(first_line) < 3:
return []
n = int(first_line[0])
k = int(first_line[1])
c = int(first_line[2])
# Process flyer information
for i in range(1, 1 + n):
if i >= len(input_lines):
break
parts = input_lines[i].split()
if len(parts) < 4:
continue
name = parts[0]
flyer_id = int(parts[1])
time = int(parts[2])
status = parts[3]
self.add_flyer(name, flyer_id, time, status)
# Process cancellations
for i in range(1 + n, 1 + n + c):
if i >= len(input_lines):
break
try:
flyer_id = int(input_lines[i])
self.cancel_flyer(flyer_id)
except ValueError:
continue
# Get top k flyers
return self.get_top_k(k)
def main():
import sys
# Read input from stdin
input_lines = []
try:
for line in sys.stdin:
if line.strip(): # Skip empty lines
input_lines.append(line.strip())
except:
pass
# If no input from stdin, use test data
if not input_lines:
# Default test case
input_lines = [
"5 2 1",
"A 1 120 Platinum",
"B 5 60 Super",
"C 3 140 Super",
"D 2 130 Gold",
"E 4 150 Silver",
"5"
]
# Create system and process input
system = NUAUpgradeSystem()
result = system.process_input(input_lines)
# Output results
for flyer in result:
print(flyer)
if __name__ == "__main__":
main()