问题描述
试题编号: | 201412-3 |
试题名称: | 集合竞价 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 某股票交易所请你编写一个程序,根据开盘前客户提交的订单来确定某特定股票的开盘价和开盘成交量。 输入格式 输入数据有任意多行,每一行是一条记录。保证输入合法。股数为不超过108的正整数,出价为精确到恰好小数点后两位的正实数,且不超过10000.00。 输出格式 你需要输出一行,包含两个数,以一个空格分隔。第一个数是开盘价,第二个是此开盘价下的成交量。开盘价需要精确到小数点后恰好两位。 样例输入 buy 9.25 100 样例输出 9.00 450 评测用例规模与约定 对于100%的数据,输入的行数不超过5000。 |
代码
#集合竞价
#cancel是从前往后执行的,不能取消cancel
import sys
records = []
delList=[]
for line in sys.stdin:
record = list(line.split())
records.append(record)
#根据cancel清除记录
for record in records:
if(record[0] == "cancel"):#用数组记录并del可能会删除两遍发生错误
record[0] = ""
records[int(record[1])-1][0] = ""#将要删除的记录和本条记录都清空
elif record[0] in ("buy","sell"):
record[1] = float(record[1])#价格
record[2] = int(record[2])#数量
for i in reversed(range(len(records))):#删除撤销记录
#print(i,records[i])
if(records[i][0]==""):
records.remove(records[i])
#print(records)
#构建buy,sell字典
records.sort(key=(lambda x:x[1]),reverse=True)#根据价格从大到小排序
buylist = {}
lastbuy=0
for bs,price,amount in records:
if(bs == "buy"):
lastbuy += amount
buylist[price]=lastbuy#存入字典
selllist = {}
lastsell=0
for bs,price,amount in records[::-1]:#价格从小到大
if(bs == "sell"):
lastsell += amount
selllist[price]=lastsell
#求竞价结果
prices=list(buylist)
max_amount=0
best_price = 0
for price in prices:
if(min(buylist[price],selllist[price])>max_amount):#买入和卖出两者的最小值为成交数
best_price=price
max_amount=min(buylist[price],selllist[price])#记录当前最大成交数
print("{:.2f} {}".format(best_price,max_amount))