#题解 称砝码 python | 遇到数据量大的case,用list超时,set则不会。

称砝码

https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c?tpId=37&&tqId=21264&rp=1&ru=/ta/huawei&qru=/ta/huawei/question-ranking

https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c?tpId=37&&tqId=21264&rp=1&ru=/ta/huawei&qru=/ta/huawei/question-ranking

# 第二行:m1 m2 m3 ... mn --- 每个砝码的重量(范围[1,2000]) 每种吧?!
# 第三行:x1 x2 x3 .... xn --- 每个砝码的数量(范围[1,6])
while 1:
    try:
        n=int(input())
        zl=list(map( int, input().split() ) )
        sl=list(map( int, input().split() ) )
        n_class = len(zl)
        if n_class==1: 
            print(sl[0]+1)
            continue
        big_lt=[
                [zl[i]*s for s in range(0, sl[i]+1) ] for i in range(n_class)    
                ]

        for i in range(n_class-2):
#           merge_two_lt
            merged=set((0,))
            #这里如果用list存数据,超时。可能是因为多了一个判断 :是否在list里
            #用set则将原本的2s,缩短至50ms左右。
            for ele_q in big_lt[0]: 
                for ele_h in big_lt[1]:
                    merged.add(ele_q+ele_h)
            big_lt.pop(0)
            big_lt[0] = merged

        merged=set((0,))
        for ele_q in big_lt[0]: 
            for ele_h in big_lt[1]:
                merged.add(ele_q+ele_h)

        print(len(merged))

    except Exception as e:
        if not isinstance(e,EOFError):
            print('leo_you_have_Error:  '  , e)
        break
全部评论

相关推荐

06-23 11:43
门头沟学院 Java
allin校招的烤冷...:我靠,今天中午我也是这个hr隔一个星期发消息给我。问的问题还是一模一样的😅
点赞 评论 收藏
分享
06-19 19:06
门头沟学院 Java
码农索隆:别去东软,真学不到东西,真事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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