解答在这:
#!/bin/python3
import math
import os
import random
import re
import sys
import bisect
import collections
# Complete the 'solve' function below.
#obj= input()
#return input()
def solve():
trade=[]
while True:
try:
trade.append(input())
except EOFError:
break
buy_limit = collections.deque()
sell_limit = collections.deque()
for i in range(1, len(trade)):
obj = trade[i].split(" ")
if obj[2]=="b" and len(sell_limit)>0:
#print("loop.{}".format(i),'buy entry')
buy_quant = int(obj[3])
buy_price = float(obj[5])
if obj[4] == "m":
while buy_quant>0 and len(sell_limit)>0:
sell_price,sell_time,sell_id,sell_quant = sell_limit.popleft()
sell_market=collections.deque()
while sell_price == float(0.00) and len(sell_limit) != 0:
bisect.insort_right(sell_market,(sell_price,sell_time,sell_id,sell_quant))
sell_price,sell_time,sell_id,sell_quant=sell_limit.popleft()
#print('lowest price',sell_price,'time'uant))
#print("loop{}".format(i),'sell',sell_limit)
else:
if obj[2]=="b":
if obj[4]=="l":
bisect.insort_right(buy_limit,(float(obj[5]),obj[0],obj[1],int(obj[3])))
else:
bisect.insort_right(buy_limit,(float('inf'),obj[0],obj[1],int(obj[3])))
#print("loop.{}".format(i),'buy',buy_limit)
else:
if obj[4]=="l":
bisect.insort_right(sell_limit,(float(obj[5]),obj[0],obj[1],int(obj[3])))
else:
bisect.insort_right(sell_limit,(float(0.00),obj[0],obj[1],int(obj[3])))
#print("loop.{}".format(i),'sell',sell_limit)
if __name__ == '__main__':
solve() |