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')

猜你喜欢

转载自blog.csdn.net/qq_39577481/article/details/83988870