fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve() {
  5. int n;
  6. cin >> n;
  7. vector<int> a(n);
  8. for (int i = 0; i < n; i++) cin >> a[i];
  9. sort(a.begin(), a.end());
  10. unordered_map<int, int> freq;
  11. for (int i = 0; i < n; i++) {
  12. freq[a[i]]++;
  13. }
  14. int l = 0, r = n - 1;
  15. while (l < r && r >= 0 && l < n) {
  16. if (freq[a[r]] % 2 == 0) {
  17. r--;
  18. } else {
  19. if (freq[a[l]] == 1) {
  20. cout << "NO" << endl;
  21. return;
  22. } else if (freq[a[l]] % 2 == 0) {
  23. l++;
  24. } else {
  25. l++;
  26. r--;
  27. freq[a[l]]--;
  28. freq[a[r]]--;
  29. }
  30. }
  31. }
  32. if (l == r) {
  33.  
  34. if (freq[a[l]] % 2 == 1) {
  35. cout << "NO" << endl;
  36. } else {
  37. cout << "YES" << endl;
  38. }
  39. } else {
  40. cout << "YES" << endl;
  41. }
  42. }
  43.  
  44. int main() {
  45. int t;
  46. cin >> t;
  47. while (t--) solve();
  48. }
Success #stdin #stdout 0.01s 5284KB
stdin
9
2
1 1
2
2 1
4
1 1 4 4
4
3 4 3 3
4
2 3 4 4
6
3 3 4 5 3 3
6
2 2 2 4 4 4
8
1 1 1 1 1 1 1 4
10
9 9 9 10 10 10 10 10 10 10
stdout
YES
NO
YES
NO
NO
NO
YES
NO
YES