错解:
for _ in range(int(input())):
n = int(input())
Ming = list(map(int, input().split()))
oppo = list(map(int, input().split()))
Ming.sort()
oppo.sort()
if Ming == oppo:
print(0)
continue
elif Ming[-1] > oppo[-1]:
print(n)
continue
elif Ming[-1] <= oppo[0]:
print(0)
for i in range(len(oppo)-1, -1, -1): # 对手马索引由大到小
if oppo[i] < Ming[-1]: # 如果对手此时马比小明弱
print(i+1)
break
else:
print(0)
正解:
for _ in range(int(input())):
n = int(input())
Ming = list(map(int, input().split()))
oppo = list(map(int, input().split()))
Ming.sort()
oppo.sort()
res = 0
i = n-1
j = n-1
while i >= 0 and j >= 0:
if oppo[i] < Ming[j]:
res += 1
i -= 1
j -= 1
else:
i -= 1
print(res)
思路:
太想简化, 忽略掉很重要的情况就是下面的值也要比较
更简单的解:
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
aa=sorted(a,reverse=True)
bb=sorted(b,reverse=True)
t=0
for i in bb:
if aa[0]>i:
aa.pop(0)
t+=1
print(t)