HULU 2019/9/5 算法笔试题目
第一题 AC 88%
def lastRemaining(n, m): if n == 1: return 0 return (lastRemaining(n-1, m) + m) % n if __name__ == "__main__": n, m = [int(x) for x in input().strip().split(' ')] a = [int(x) for x in input().strip().split(' ')] w = [int(x) for x in input().strip().split(' ')] tol = sum(w) num = lastRemaining(n, m) pro = 0 for i in range(len(a)): if a[num] == 1: pro += w[i]/tol num = (num + 1) % n print("%.5f"%pro)第二题 AC 100%
def solve(A): MOD = 10**9 + 7 stack = [] ans = dot = 0 for j, y in enumerate(A): count = 1 while stack and stack[-1][0] <= y: x, c = stack.pop() count += c dot -= x * c stack.append((y, count)) dot += y * count ans += dot return ans % MOD if __name__ == '__main__': n = int(input()) nums = list(map(int,input().strip().split(' '))) print(solve(nums))第三题 AC 36% 用的动态规划,一直没明白 为什么通不过所有样例,内存优化了,而且也没有超时
if __name__ == "__main__": n = int(input()) net = [0]*n for i in range(n): t = [int(x) for x in input().strip().split(' ')] if i == 0: for j in range(1, n): net[j] = net[j-1] + t[j] continue for j in range(n): if j == 0: net[j] = net[j] + t[j] continue net[j] = min(net[j-1], net[j]) + t[j] print(net[n-1])
第四题 放弃
相求 大佬指点下 第一题和 第三题 为啥 没有 AC 100%