fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Speed
  5. #define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  6.  
  7. // Typedefs
  8. #define int long long
  9. #define pb push_back
  10. #define ff first
  11. #define ss second
  12. #define all(x) (x).begin(), (x).end()
  13. #define rall(x) (x).rbegin(), (x).rend()
  14. #define sz(x) ((int)(x).size())
  15. #define endl '\n'
  16. #define yes cout << "yes\n"
  17. #define no cout << "no\n"
  18.  
  19. // Loops
  20. #define rep(i,a,b) for(int i=a;i<b;++i)
  21. #define per(i,a,b) for(int i=b-1;i>=a;--i)
  22. #define each(x, a) for (auto& x : a)
  23.  
  24. // Consts
  25. const int INF = 1e18;
  26. const int MOD = 1e9+7;
  27. const int N = 2e5 + 5;
  28.  
  29. // Math
  30. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  31. int lcm(int a, int b) { return (a / gcd(a, b)) * b; }
  32.  
  33. int power(int a, int b, int m = MOD) {
  34. int res = 1;
  35. while (b > 0) {
  36. if (b & 1) res = res * a % m;
  37. a = a * a % m;
  38. b >>= 1;
  39. }
  40. return res;
  41. }
  42.  
  43. int modinv(int a, int m = MOD) {
  44. return power(a, m - 2, m);
  45. }
  46.  
  47. // Helper: compute MEX of 3-element vector
  48. int get_mex(vector<int> v) {
  49. set<int> s(all(v));
  50. for (int i = 0; i <= 4; ++i) {
  51. if (!s.count(i)) return i;
  52. }
  53. return 5; // not needed
  54. }
  55.  
  56. bool is_good_triplet(vector<int>& triplet) {
  57. int mn = *min_element(all(triplet));
  58. int mx = *max_element(all(triplet));
  59. return get_mex(triplet) == (mx - mn);
  60. }
  61.  
  62. bool can_make_good(vector<int>& a) {
  63. int n = sz(a);
  64. rep(i, 0, n - 2) {
  65. vector<int> sub = {a[i], a[i + 1], a[i + 2]};
  66. int missing = count(all(sub), -1);
  67.  
  68. if (missing == 0) {
  69. if (!is_good_triplet(sub)) return false;
  70. continue;
  71. }
  72.  
  73. bool found = false;
  74. rep(x, 0, 4) {
  75. rep(y, 0, 4) {
  76. rep(z, 0, 4) {
  77. vector<int> temp = sub;
  78. if (temp[0] == -1) temp[0] = x;
  79. if (temp[1] == -1) temp[1] = y;
  80. if (temp[2] == -1) temp[2] = z;
  81. if (is_good_triplet(temp)) {
  82. found = true;
  83. goto next_triplet;
  84. }
  85. }
  86. }
  87. }
  88. next_triplet:
  89. if (!found) return false;
  90. }
  91. return true;
  92. }
  93.  
  94. // Logic
  95. void solve() {
  96. int n;
  97. cin >> n;
  98. vector<int> a(n);
  99. rep(i, 0, n) cin >> a[i];
  100. if (can_make_good(a)) yes;
  101. else no;
  102. }
  103.  
  104. // Main
  105. int32_t main() {
  106. fast_io;
  107.  
  108. int t;
  109. cin >> t;
  110. while (t--) {
  111. solve();
  112. }
  113.  
  114. return 0;
  115. }
  116.  
Success #stdin #stdout 0.01s 5284KB
stdin
8
3
-1 -1 -1
5
1 1 1 1 0
6
5 5 1 -1 -1 1
4
-1 -1 0 -1
4
-1 1 1 -1
3
3 3 -1
5
0 0 0 0 0
7
3 0 1 4 -1 2 3
stdout
yes
no
no
no
yes
yes
no
no