阿里笔试330
感觉自己是个智障,不知道为什么都没有ac,第二题是题目看错了,前面写成所有可能的subset的expectation了。之前没有参加过国内的笔试,求大佬来看看
第一题
import sys import heapq n, m, k = 0, 0, 0 farm = {} if __name__ == "__main__": # 读取第一行的n n = 2 ans = 0 for i in range(n): # 读取每一行 line = sys.stdin.readline().strip() # 把每一行的数字分隔后转化成int列表 values = list(map(int, line.split())) if i==0: n,m,k = values[0],values[1],values[2] else: farm = values heap = [] for i in range(len(farm)): heapq.heappush(heap,-1*farm[i]) for i in range(1,m+1): x = heapq.heappop(heap) x = (x-i*k)/2 x = x+i*k heapq.heappush(heap,x) print(heap) for i in range(len(heap)): heap[i] -= m*k print(-1*int(sum(heap)))
第二题
import sys n = 0 input_val = [] if __name__ == "__main__": # 读取第一行的n n = 2 ans = 0 for i in range(n): # 读取每一行 line = sys.stdin.readline().strip() # 把每一行的数字分隔后转化成int列表 values = list(map(int, line.split())) if i==0: n =values[0] else: input_val = values #一个长为n的序列,求随机连续子序列的最大值的期望 #3 #1 2 3 input_val = sorted(input_val) dp = [0]*n dp[0] = 1 for i in range(1,n): if input_val[i]==input_val[i-1]+1: dp[i] = dp[i-1]+1 else: dp[i] = 1 print(dp) sum_val,count_val = 0,0 for i in range(len(dp)): sum_val += dp[i]*input_val[i] count_val += dp[i] print (sum_val/count_val)