快手
笔试-快手-180910
单选 20;编程 3
Index
字符串归一化
Python(AC)
魔法深渊
Python(AC)
善变的同伴
思路
Python(未测试)
按照上述思路实现的代码
```
N, M = list(map(int, input().split()))
ns = list(map(int, input().split()))
N, M = 7, 3
ns = [1, 2, 3, -2, 3, -10, 3]
new_ns = [ns[0]]
for i in ns[1:]: if i == 0: continue if (new_ns[-1] > 0) == (i > 0): new_ns[-1] += i else: new_ns.append(i)
print(new_ns) # [6, -2, 3, -10, 3]
去掉首尾的负数块
if len(new_ns) >= 1 and new_ns[0] < 0: new_ns.pop(0)
if len(new_ns) >= 1 and new_ns[-1] < 0: new_ns.pop(-1)
print(new_ns) # [6, -2, 3, -10, 3]
按奇偶划分奇数和偶数块
ns_pos = new_ns[0::2] ns_neg = new_ns[1::2] cnt_pos = len(ns_pos)
print(cnt_pos) # 3
print(ns_pos) # [6, 3, 3]
print(ns_neg) # [-2, -10]
如果 M 的值小于正数块的数量则进行合并
updated = True while updated and M < cnt_pos: """""" updated = False
ns_pos.sort(reverse=True) print(sum(ns_pos[:M])) ```
最后更新于