题解 | 支付宝消费打折

支付宝消费打折

https://www.nowcoder.com/practice/f8997c9b82714f058e12433a32614993

import sys

lines = sys.stdin.readlines()
n,k = map(int,lines[0].strip().split())
price = list(map(int,lines[1].strip().split()))
discounts = lines[2].strip()

goods = []
for i in range(1,len(price)+1):
    discount = 95 if discounts[i-1] == '1' else 100
    goods.append([i,price[i-1] * discount])

goods.sort(key = lambda x:x[1])
remain = k * 100
count = 0
for i in range(len(goods)):
    if remain - goods[i][1] >= 0:
        remain -= goods[i][1]
        count += 1
    else:
        break
print(count)

1.多次运用优先级排序

2.计数:在满足条件(不超过余额)时+1,不满足条件则break

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务