fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5.  
  6. #define loop(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
  7.  
  8. void solve() {
  9. int n;
  10. cin >> n;
  11. vector<int> a(n);
  12.  
  13. unordered_map<int, int> freq;
  14. loop(i, 0, n)
  15. {
  16. cin >> a[i];
  17. freq[a[i]]++;
  18. }
  19. sort(a.begin(), a.end());
  20.  
  21. int prev = a[0];
  22. for (int i = 1; i < n; i++) {
  23. if (a[i] != prev) {
  24.  
  25. int diff = a[i] - prev;
  26.  
  27. if (freq[prev] >= 2) {
  28.  
  29. if (freq[prev] - 1 >= diff * 2) {
  30.  
  31. freq[a[i]] += freq[prev] - 2 - (diff - 1) * 2;
  32.  
  33. } else if (freq[prev] & 1) {
  34. cout << "NO" << endl;
  35. return;
  36. }
  37.  
  38. } else {
  39. cout << "NO" << endl;
  40. return;
  41. }
  42.  
  43. prev = a[i];
  44. }
  45. }
  46. cout << "YES" << endl;
  47. }
  48.  
  49. int main() {
  50. ios::sync_with_stdio(false);
  51. cin.tie(nullptr);
  52. int t;
  53. cin >> t;
  54. while (t--) solve();
  55. }
Success #stdin #stdout 0.01s 5288KB
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
YES
NO
YES
NO
YES
YES