PAT 乙级 1020 月饼

月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。

注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨,总售价分别为 75、72、45 亿元。如果市场的最大需求量只有 20 万吨,那么我们最大收益策略应该是卖出全部 15 万吨第 2 种月饼、以及 5 万吨第 3 种月饼,获得 72 + 45/2 = 94.5(亿元)。

输入格式:

每个输入包含一个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N 表示月饼的种类数、以及不超过 500(以万吨为单位)的正整数 D 表示市场最大需求量。随后一行给出 N 个正数表示每种月饼的库存量(以万吨为单位);最后一行给出 N 个正数表示每种月饼的总售价(以亿元为单位)。数字间以空格分隔。

输出格式:

对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后 2 位。

Python(1)(测试点1运行超时) 

kind, need = map(int,input().split())
own = list(map(float,input().split()))
price = list(map(float,input().split()))
most = 0
cnt = 0
t = [i for i in range(kind)]

def sorting(t,price,own):
    for i in range(kind-1):
        for j in range(kind-i-1):
            if price[t[j]] < price[t[j+1]]:
                t[j], t[j+1] = t[j+1], t[j]
    return t

if sum(own) <= need:
    print('%.2f'%sum(price))
else:
    for i in range(kind):
        price[i] = price[i] / own[i]
    t = sorting(t,price,own)
    for i in range(kind):
        if own[t[i]] + cnt <= need:
            cnt += own[t[i]]
            most += own[t[i]] * price[t[i]]
        else:
           most += (need - cnt) * price[t[i]]
           cnt = need
           break
    print('%.2f'%most)

Python(2)

kind, need = map(int,input().split())
goods = [[],[]]
goods[0] = list(map(float,input().split())) #own
goods[1] = list(map(float,input().split())) #price
most = 0
cnt = 0

if sum(goods[0]) <= need:
    print('%.2f'%sum(goods[1]))
else:
    temp = [[[] for i in range(2)] for i in range(kind)]
    for i in range(2):
        for j in range(kind):
            temp[j][i] = goods[i][j]
    goods = temp
    for i in range(kind):
        goods[i][1] = goods[i][1] / goods[i][0]
    goods.sort(key = lambda x : x[1], reverse = True)
    for i in range(kind):
        if goods[i][0] + cnt <= need:
            cnt += goods[i][0]
            most += goods[i][0] * goods[i][1]
        else:
           most += (need - cnt) * goods[i][1]
           cnt = need
           break
    print('%.2f'%most)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值