[关闭]
@w616561153 2020-01-27T23:34:01.000000Z 字数 1583 阅读 382

20141203

未分类


  1. import sys
  2. a = []
  3. b = []
  4. c = set()
  5. cnt = int(0)
  6. '''
  7. for i in range(10):
  8. s = input().split()
  9. a.append(s)
  10. '''
  11. for line in sys.stdin:
  12. s = line.split()
  13. a.append(s)
  14. b = a.copy()
  15. for x in a:
  16. if x[0][0] == 'c':
  17. del b[int(x[1]) - 1 - cnt]
  18. cnt += 1
  19. for i in b:
  20. if i[0][0] != 'c':
  21. c.add(float(i[1]))
  22. c.add(0.0)
  23. c = list(c)
  24. c.sort(reverse = True)
  25. ans = int(0)
  26. for i in c:
  27. sum_buy , sum_sell = 0 , 0
  28. for j in b:
  29. if j[0][0] == 'b' and float(j[1]) >= i:
  30. sum_buy += int(j[2])
  31. if j[0][0] == 's' and float(j[1]) <= i:
  32. sum_sell += int(j[2])
  33. if min(sum_sell, sum_buy) > ans:
  34. t = i
  35. ans = min(sum_sell, sum_buy)
  36. print("{:.2f} {}".format(t, ans))

  1. #集合竞价
  2. #cancel是从前往后执行的,不能取消cancel
  3. import sys
  4. records = []
  5. delList=[]
  6. for line in sys.stdin:
  7. record = list(line.split())
  8. records.append(record)
  9. #根据cancel清除记录
  10. for record in records:
  11. if(record[0] == "cancel"):#用数组记录并del可能会删除两遍发生错误
  12. record[0] = ""
  13. records[int(record[1])-1][0] = ""#将要删除的记录和本条记录都清空
  14. elif record[0] in ("buy","sell"):
  15. record[1] = float(record[1])#价格
  16. record[2] = int(record[2])#数量
  17. for i in reversed(range(len(records))):#删除撤销记录
  18. #print(i,records[i])
  19. if(records[i][0]==""):
  20. records.remove(records[i])
  21. #print(records)
  22. #构建buy,sell字典
  23. records.sort(key=(lambda x:x[1]),reverse=True)#根据价格从大到小排序
  24. buylist = {}
  25. lastbuy=0
  26. for bs,price,amount in records:
  27. if(bs == "buy"):
  28. lastbuy += amount
  29. buylist[price]=lastbuy#存入字典
  30. selllist = {}
  31. lastsell=0
  32. for bs,price,amount in records[::-1]:#价格从小到大
  33. if(bs == "sell"):
  34. lastsell += amount
  35. selllist[price]=lastsell
  36. #求竞价结果
  37. prices=list(buylist)
  38. max_amount=0
  39. best_price = 0
  40. for price in prices:
  41. if(min(buylist[price],selllist[price])>max_amount):#买入和卖出两者的最小值为成交数
  42. best_price=price
  43. max_amount=min(buylist[price],selllist[price])#记录当前最大成交数
  44. print("{:.2f} {}".format(best_price,max_amount))

https://blog.csdn.net/SL_logR/article/details/82696692

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注