// Marcin Knapik #include <bits/stdc++.h> using namespace std; #define FOR(i, b) for (int i = 0; i < b; ++i) #define sz(s) (int)s.size() #define all(s) s.begin(), s.end() #define mp make_pair #define pb push_back #define f first #define s second void tak() { cout << "TAK\n"; } void nie() { cout << "NIE\n"; } void solve() { int n; cin >> n; int first_pos = n - 1; int last_pos = -1; vector<int> tab(n); long long sum = 0; FOR(i, n) { cin >> tab[i]; if (tab[i] != 0) { first_pos = min(first_pos, i); last_pos = max(last_pos, i); } sum += tab[i]; } auto empty_inside = [&]() -> bool { for (int i = first_pos; i <= last_pos; ++i) { if (tab[i] == 0) { return true; } } return false; }; if (empty_inside()) { nie(); return; } if (first_pos == last_pos) { if (tab[first_pos] == 1) { tak(); } else { nie(); } return; } auto works = [first_pos, last_pos](vector<int> &vec) -> bool { for (int i = first_pos; i <= last_pos; ++i) { if (vec[i] < 0) { return false; } if (vec[i] > 0) { if (i == last_pos) { return false; } vec[i + 1] -= vec[i]; // vec[i] = 0; } } return true; }; if ((sum - first_pos - last_pos + 1) % 2 == 0) { vector<int> option1 = tab; for (int i = first_pos; i <= last_pos; ++i) { --option1[i]; } vector<int> option2 = option1; --option2[first_pos + 1]; --option2[last_pos - 1]; if (works(option1) || works(option2)) { tak(); } else { nie(); } return; } else { vector<int> option1 = tab; for (int i = first_pos; i <= last_pos; ++i) { --option1[i]; } --option1[first_pos + 1]; vector<int> option2 = tab; for (int i = first_pos; i <= last_pos; ++i) { --option2[i]; } --option2[last_pos - 1]; if (works(option1) || works(option2)) { tak(); } else { nie(); } return; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; cin >> t; FOR(test, t) { solve(); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | // Marcin Knapik #include <bits/stdc++.h> using namespace std; #define FOR(i, b) for (int i = 0; i < b; ++i) #define sz(s) (int)s.size() #define all(s) s.begin(), s.end() #define mp make_pair #define pb push_back #define f first #define s second void tak() { cout << "TAK\n"; } void nie() { cout << "NIE\n"; } void solve() { int n; cin >> n; int first_pos = n - 1; int last_pos = -1; vector<int> tab(n); long long sum = 0; FOR(i, n) { cin >> tab[i]; if (tab[i] != 0) { first_pos = min(first_pos, i); last_pos = max(last_pos, i); } sum += tab[i]; } auto empty_inside = [&]() -> bool { for (int i = first_pos; i <= last_pos; ++i) { if (tab[i] == 0) { return true; } } return false; }; if (empty_inside()) { nie(); return; } if (first_pos == last_pos) { if (tab[first_pos] == 1) { tak(); } else { nie(); } return; } auto works = [first_pos, last_pos](vector<int> &vec) -> bool { for (int i = first_pos; i <= last_pos; ++i) { if (vec[i] < 0) { return false; } if (vec[i] > 0) { if (i == last_pos) { return false; } vec[i + 1] -= vec[i]; // vec[i] = 0; } } return true; }; if ((sum - first_pos - last_pos + 1) % 2 == 0) { vector<int> option1 = tab; for (int i = first_pos; i <= last_pos; ++i) { --option1[i]; } vector<int> option2 = option1; --option2[first_pos + 1]; --option2[last_pos - 1]; if (works(option1) || works(option2)) { tak(); } else { nie(); } return; } else { vector<int> option1 = tab; for (int i = first_pos; i <= last_pos; ++i) { --option1[i]; } --option1[first_pos + 1]; vector<int> option2 = tab; for (int i = first_pos; i <= last_pos; ++i) { --option2[i]; } --option2[last_pos - 1]; if (works(option1) || works(option2)) { tak(); } else { nie(); } return; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; cin >> t; FOR(test, t) { solve(); } return 0; } |