9月1日 腾讯算法 笔试
第一题 AC 100%
if __name__ == "__main__": T = int(input()) while T > 0: n = int(input()) a = [int(x) for x in input().strip().split(' ')] if len(a) % 2 == 1: print('NO') else: a.sort() i = 0 v = 0 while i < len(a)-1: j = i while j < len(a) and a[j] == a[i]: j += 1 v = max(v, j-i) i = j if v > len(a) // 2: print('NO') else: print('YES') T -= 1
第二题 不知道 运行 结果如何 ,考试 一直 超时 ,后来优化了下
if __name__ == "__main__": t, k = [int(x) for x in input().strip().split(' ')] minv = float('inf') maxv = -float('inf') a_b = [] for i in range(t): a, b = [int(x) for x in input().strip().split(' ')] a_b.append((a, b)) minv = min(a, minv) maxv = max(b, maxv) res = [0] * (maxv+1) for i in range(minv, maxv+1): res[i] = 1 if i < k: continue j = i // k fir = i+1-1*k las = i+1-j*k res[i] += (fir+las)*j // 2 while t > 0: a, b = a_b.pop(0) print(sum(res[a:b+1])) t -= 1
def func(nums, T): res = 0 for n in nums: cun = len(n) i = 0 flag = False for t in T: if i == cun: i = 0 if n[i] == t: i += 1 else: flag = True break if not flag: res += 1 print(res) n = int(input()) T = input() nums = [] s = int(input()) for _ in range(s): nums.append(input()) func(nums, T)