fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main() {
  7. cin.tie(0)->sync_with_stdio(0);
  8. int t; cin >> t;
  9. while (t--) {
  10. int n, k; cin >> n >> k;
  11. vector<int> a(n);
  12. for (int &x : a) cin >> x;
  13. vector<int> b(a);
  14. sort(b.begin(), b.end());
  15. int target = b[k - 1];
  16.  
  17. vector<int> c;
  18. for (int x : a) {
  19. if (x <= target) c.push_back(x);
  20. }
  21.  
  22. int rem = (int) c.size();
  23.  
  24. int l = 0, r = rem - 1;
  25. bool works = true;
  26. while (l <= r) {
  27. if (c[l] != c[r]) {
  28. if (c[l] == target) {
  29. rem--;
  30. l++;
  31. } else if (c[r] == target) {
  32. rem--;
  33. r--;
  34. } else {
  35. works = false;
  36. break;
  37. }
  38. } else {
  39. l++;
  40. r--;
  41. }
  42. }
  43. cout << ((works && rem >= k - 1) ? "YES" : "NO") << '\n';
  44. }
  45. }
  46.  
  47.  
  48.  
Success #stdin #stdout 0.01s 5320KB
stdin
8
5 3
5 4 3 4 5
4 1
1 1 2 1
6 6
2 3 4 5 3 2
5 4
5 2 4 3 1
8 5
4 7 1 2 3 1 3 4
5 4
1 2 1 2 2
3 3
1 2 2
4 4
2 1 2 2
stdout
YES
YES
YES
NO
NO
YES
NO
YES