#include <iostream> #include <vector> #include <algorithm> bool findPairWithDifferenceK(std::vector<int>& arr, int k) { std::sort(arr.begin(), arr.end()); int p = 0; int q = 1; while (q < arr.size()) { if (arr[q] - arr[p] < k) { q++; } else if (arr[q] - arr[p] > k) { p++; } else { return true; } if (p == q) { q++; } } return false; } int main() { int N, Q; std::cin >> N >> Q; std::vector<int> heights(N); for (int i = 0; i < N; ++i) { std::cin >> heights[i]; } for (int i = 0; i < Q; ++i) { int K; std::cin >> K; if (findPairWithDifferenceK(heights, K)) { std::cout << "YES" << std::endl; } else { std::cout << "NO" << std::endl; } } return 0; }
在最後一個測資點TLE。