网易 2019内推 笔试编程题 |1|

网易笔试编程题1

背景:小易上课打瞌睡,但是又不想错过自己喜欢的内容,于是将一节课划分为n个时间段,每个时间段根据小易的兴趣给定一个评分,每节课可以叫醒小易一次,叫醒一次保持k个时间段,怎么使小易这个课评分最高?
输入:
n,k
小易每个时间段兴趣评分
小易每个时间段是否瞌睡:0,瞌睡;1,清醒
示例1:
6 3
2 3 4 5 3 1
1 1 0 1 0 0

17
示例2:
6 3
2 3 4 5 3 1
1 1 1 1 0 0

18
代码如下:

# encoding=utf-8
import copy
n,k = [int(each) for each in input().split()]
a = [int(each) for each in input().split()]
b = [int(each) for each in input().split()]

def Find(n,k,a,b):
    len_a = len(a)
    len_b = len(b)
    sord = []
    for i in range(len_b-k+1):
        sum = 0
        c = copy.deepcopy(b)
        if c[i] == 0:
            for j in range(i,i+k):
                c[j] = 1
            for j in range(len_a):
                sum = sum + a[j]*c[j]
            sord.append(sum)
    if b[len_b-k] != 0:
        c = copy.deepcopy(b)
        sum = 0
        for j in range(len_b-k+1,len_b):
            c[j] = 1
        for j in range(len_a):
            sum = sum + a[j]*c[j]
        sord.append(sum)
    result = max(sord)
    return result
print(Find(n,k,a,b))

你可能感兴趣的:(网易 2019内推 笔试编程题 |1|)