B. Subtract Operation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a list of nn integers. You can perform the following operation: you choose an element xx from the list, erase xx from the list, and subtract the value of xx from all the remaining elements. Thus, in one operation, the length of the list is decreased by exactly 11.
Given an integer kk (k>0k>0), find if there is some sequence of n−1n−1 operations such that, after applying the operations, the only remaining element of the list is equal to kk.
Input
The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers nn and kk (2≤n≤2⋅1052≤n≤2⋅105, 1≤k≤1091≤k≤109), the number of integers in the list, and the target value, respectively.
The second line of each test case contains the nn integers of the list a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109).
It is guaranteed that the sum of nn over all test cases is not greater that 2⋅1052⋅105.
Output
For each test case, print YES if you can achieve kk with a sequence of n−1n−1 operations. Otherwise, print NO.
You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as a positive answer).
Example
input
Copy
4 4 5 4 2 2 7 5 4 1 9 1 3 4 2 17 17 0 2 17 18 18
output
Copy
YES NO YES NO
Note
In the first example we have the list {4,2,2,7}{4,2,2,7}, and we have the target k=5k=5. One way to achieve it is the following: first we choose the third element, obtaining the list {2,0,5}{2,0,5}. Next we choose the first element, obtaining the list {−2,3}{−2,3}. Finally, we choose the first element, obtaining the list {5}{5}.
import sys
import math
#import numpy as np
from collections import defaultdict
#sys.stdin = open("in.txt", "r")
T = int(input())
for _ in range(T):
n, l = [int(i) for i in input().split()]
arr=[int(i) for i in input().split()]
dict=defaultdict(int)
for v in arr:
dict[v]=1
flag=True
for v in arr:
if (v-l) in dict or (v+l) in dict:
print('YES')
flag=False
break
if flag:
print('NO')
只要存在差值为k的pair就可以,每次操作可以移除其他数
本文探讨了一种特殊的列表操作问题,即通过一系列操作使列表中的最后一个元素等于预设的目标值k。文章详细介绍了问题背景、输入输出格式及示例,并提供了一个算法实现思路。
3003

被折叠的 条评论
为什么被折叠?



