CCF Python题解(100分)201412-3 集合竞价

CCF Python题解(100分)201412-3 集合竞价

from collections import defaultdict

records = []


def zero():
    return 0


buy = defaultdict(zero)
sell = defaultdict(zero)
okdict = {}

while True:
    try:
        record = input().split()

        if record:
            # record[0] cancel
            if record[0] == 'cancel':

                if records[int(record[1]) - 1][0].endswith('c'):
                    records[int(record[1]) - 1][0].rstrip('c')
                else:

                    records[int(record[1]) - 1][0] += 'c'

        records.append(record)
    except:
        break

for index in range(len(records)):
    record = records[index]
    if record:
        if record[0] == 'buy':
            buy[record[1]] += int(record[2])
        elif record[0] == 'sell':
            sell[record[1]] += int(record[2])
newbuy = sorted(buy.items(), key=lambda buy: float(buy[0]), reverse=True)  # 按键降序
newsell = sorted(sell.items(), key=lambda sell: float(sell[0]), reverse=False)  # 按键升序

for index in range(len(newbuy)):
    sbuy = 0
    ssell = 0

    for i in range(index + 1):
        sbuy += newbuy[i][1]

    for j in newsell:
        if j[0] <= newbuy[index][0]:
            ssell += j[1]
        else:
            break

    okdict[newbuy[index][0]] = min(sbuy, ssell)

oklist = sorted(okdict.items(), key=lambda okdict: okdict[1], reverse=True)  # 按值降序
if oklist:
    if oklist[0][1] == 0:
        print('0.00 0')
    else:
        print(oklist[0][0],  oklist[0][1])

else:
    print('0.00 0')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值