每个测试文件均包含多组测试数据。第一行输入一个整数
代表数据组数,每组测试数据描述如下:
第一行输入两个整数
代表数组长度、你需要取出的元素数量。
第二行输入
个整数
代表数组元素。
除此之外,保证单个测试文件的
之和不超过
。
对于每一组测试数据,如果存在至少一种方案,使得最终得到的数组是个严格递增或严格递减数组,在单独的一行上输出
;否则,直接输出
。
4 5 5 1 2 3 4 5 3 2 9 2 4 6 3 12 3 8 7 6 5 5 3 1 3 2 4 5
YES YES YES NO
对于第一组测试数据,不需要进行任何操作,数组已经是严格递增数组。
对于第二组测试数据,选择区间
,随后将这两个元素插入到
之前,得到
。
对于第三组测试数据,符合要求的方案为,选择区间
,随后将剩余的元素重新排列得到
,随后将这三个元素插入到
之后,得到
。
T = int(input())
def ismonotone(num_list):
if num_list[0] > num_list[-1]:
return all(num_list[i - 1] > num_list[i] for i in range(1, len(num_list)))
else:
return all(num_list[i - 1] < num_list[i] for i in range(1, len(num_list)))
for i in range(T):
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
output_str = "NO"
if len(set(a)) != len(a):
print("NO")
elif n == m:
print("YES" if ismonotone(a) else "NO")
elif m == 1:
print("YES")
elif n-m==1:
s_1,s_2=a[:-1],a[1:]
if (ismonotone(a[:-1]) and (max(a)==a[-1]&nbs***bsp;min(a)==a[-1]))&nbs***bsp;(ismonotone(a[1:]) and (max(a)==a[0]&nbs***bsp;min(a)==a[0])):
output_str = "YES"
print(output_str)
else:
for i in range(n - m + 1):
s = a[i: i + m]
if ismonotone(s):
t = sorted(a[:i] + a[i + m:]) if s[0]<s[-1] else sorted(a[:i] + a[i + m:],reverse=True)
if max(s)<min(t)&nbs***bsp;min(s)>max(t):
output_str = "YES"
else:
for j in range(1,len(t)):
if (t[j-1]>s[0] and t[j]<s[-1] and s[0]>s[-1])&nbs***bsp;(s[0]<s[-1] and t[j-1]<s[0] and t[j]>s[-1]):
output_str="YES"
break
if output_str=="YES":
break
print(output_str) 写得有点点屎山,但跑得还挺顺畅