网易算法岗 60 100 100 60(python2)
求一四题指教!!!!!
第一题(吃葡萄)(60% 答案错误)
import sys def ceil(n): ans = int(n) + 1 if n != int(n) else int(n) return ans T = int(raw_input()) for t in xrange(T): a, b, c = map(float, raw_input().split()) ans = max(map(ceil, [(a+b+c) / 3.0, a / 2.0, b / 2.0, c / 2.0])) mod = 10 ** 9 + 7 if ans >= mod: print ans % mod else: print ans第二题(搭积木)(100%)
import sys T = int(raw_input()) for _ in xrange(T): n, m = map(int, raw_input().split()) h = map(int, raw_input().split()) flag = True for i in range(n): m = m + h[i] - i if m < 0: flag = False break print 'YES' if flag else 'NO'
第三题(跳梯子)(100%)
import sys T = int(raw_input()) for _ in xrange(T): n, k = map(int, raw_input().split()) h = map(int, raw_input().split()) using = [False] * n notusing = [False] * n using[0], notusing[0] = True, True for i in range(1, n): j = max(0, i - k) while j < i: if notusing[j] and h[i] <= h[j]: notusing[i] = True if notusing[j] or (using[j] and h[i] <= h[j]): using[i] = True if using[i] and notusing[i]: break j += 1 if using[-1] or notusing[-1]: print 'YES' else: print 'NO'
第四题(逆序对)——解法1(60% 超时)
import sys n = int(raw_input()) nums = map(int, raw_input().split()) ans = 0 for i in range(1, n): a = nums[i] for j in range(i): b = nums[j] if a < b: ans += i - j print ans
第四题——解法2(0% 答案错误)
import sys n = int(raw_input()) nums = map(int, raw_input().split()) if n == 1: print 0 else: ans = 0 h = [(0, nums[0])] for i, a in enumerate(nums[1:]): tmp = [] j = len(h) - 1 ind, pre = h[j] if pre < a: h.append((i, a)) else: while pre > a: ans += i - ind j -= 1 if j < 0: break ind, pre = h[j] h.insert(j+1, (i, a)) print ans