#include <bits/stdc++.h> using namespace std; #ifdef DEBUG #include "debug.hpp" #else #define debug(...) (void)0 #endif namespace R = ranges; namespace V = views; auto ra(auto x, auto y) { return R::iota_view(x, y); } auto rae(auto x, auto y) { return ra(x, y + 1); } using i64 = int64_t; void solve() { int n; cin >> n; vector<int> a(n); for (auto &ai : a) { cin >> ai; } { vector<int> new_a; for (auto i : ra(0, n)) { if (a[i] == 0 and empty(new_a)) { continue; } new_a.emplace_back(a[i]); } while (new_a.back() == 0) { new_a.pop_back(); } a = std::move(new_a); n = ssize(a); if (count(begin(a), end(a), 0) > 0) { cout << "NIE\n"; return; } } for (auto rep : ra(0, 2)) { for (auto s : ra(0, n)) { debug(s); auto b = a; for (auto i : ra(s, n)) { b[i] -= 1; } debug(b); for (auto i : ra(1, n) | V::reverse) { int x = b[i]; if (x > 0) { b[i] -= x; b[i - 1] -= x; } b[i - 1] -= 1; } debug(b); for (auto i : ra(0, s)) { int x = b[i]; if (x > 0) { b[i] -= x; b[i + 1] -= x; } b[i + 1] -= 1; } debug(b); int n0 = count(begin(b), end(b), 0); if (n0 == n) { cout << "TAK\n"; return; } int nm1 = count(begin(b), end(b), -1); if (s + 1 >= nm1 and nm1 + n0 == n) { if (count(begin(b) + s - nm1 + 1, begin(b) + s + 1, -1) == nm1) { cout << "TAK\n"; return; } } } R::reverse(a); } cout << "NIE\n"; } int32_t main() { cin.tie(nullptr)->sync_with_stdio(false); int t = 1; cin >> t; for (auto tc_n : ra(0, t)) { debug(tc_n); solve(); cout.flush(); } }
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 | #include <bits/stdc++.h> using namespace std; #ifdef DEBUG #include "debug.hpp" #else #define debug(...) (void)0 #endif namespace R = ranges; namespace V = views; auto ra(auto x, auto y) { return R::iota_view(x, y); } auto rae(auto x, auto y) { return ra(x, y + 1); } using i64 = int64_t; void solve() { int n; cin >> n; vector<int> a(n); for (auto &ai : a) { cin >> ai; } { vector<int> new_a; for (auto i : ra(0, n)) { if (a[i] == 0 and empty(new_a)) { continue; } new_a.emplace_back(a[i]); } while (new_a.back() == 0) { new_a.pop_back(); } a = std::move(new_a); n = ssize(a); if (count(begin(a), end(a), 0) > 0) { cout << "NIE\n"; return; } } for (auto rep : ra(0, 2)) { for (auto s : ra(0, n)) { debug(s); auto b = a; for (auto i : ra(s, n)) { b[i] -= 1; } debug(b); for (auto i : ra(1, n) | V::reverse) { int x = b[i]; if (x > 0) { b[i] -= x; b[i - 1] -= x; } b[i - 1] -= 1; } debug(b); for (auto i : ra(0, s)) { int x = b[i]; if (x > 0) { b[i] -= x; b[i + 1] -= x; } b[i + 1] -= 1; } debug(b); int n0 = count(begin(b), end(b), 0); if (n0 == n) { cout << "TAK\n"; return; } int nm1 = count(begin(b), end(b), -1); if (s + 1 >= nm1 and nm1 + n0 == n) { if (count(begin(b) + s - nm1 + 1, begin(b) + s + 1, -1) == nm1) { cout << "TAK\n"; return; } } } R::reverse(a); } cout << "NIE\n"; } int32_t main() { cin.tie(nullptr)->sync_with_stdio(false); int t = 1; cin >> t; for (auto tc_n : ra(0, t)) { debug(tc_n); solve(); cout.flush(); } } |