// clang-format off
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(auto i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<"("<<p.first<<", "<<p.second<<")";}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";}
#ifdef DEBUG
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<"\n";}(X)
#else
#define debug(...) {}
#endif
// clang-format on
int T, N, max_val;
vector<int> seq(N);
inline void
insert(pair<int, int>& p, int val, int limit)
{
if (val < 1 || val > limit || val == p.first || val == p.second) return;
if (val > p.first) {
swap(p.first, p.second);
p.first = val;
} else if (val > p.second) {
p.second = val;
}
}
void
calc_dp(vector<int>& go_with_ret, vector<pair<int, int>>& go_end)
{
go_with_ret.resize(N + 2, -1);
go_with_ret[0] = 0;
FOR (n, 1, N) {
const auto& a = seq[n - 1];
auto& curr = go_with_ret[n];
curr = a - go_with_ret[n - 1];
if (curr < 1 || curr > a) curr = -1;
}
go_end.resize(N + 2);
FOR (n, 1, N) {
const auto& a = seq[n - 1];
insert(go_end[n], go_with_ret[n], a);
insert(go_end[n], a - go_end[n - 1].first + 1, a);
insert(go_end[n], a - go_end[n - 1].second + 1, a);
}
}
bool
solve()
{
cin >> N;
seq.resize(N);
for (auto& x : seq) cin >> x;
// Crop zeros
{
while (seq.back() == 0) seq.pop_back();
auto l = seq.begin();
while (*l == 0) ++l;
seq.assign(l, seq.end());
N = ssize(seq);
}
// Preprocessing
max_val = 0;
REP (i, N) {
if (seq[i] == 0) return false;
max_val = max(max_val, seq[i]);
}
debug(seq);
vector<int> go_with_ret[2]; // # to go to visit whole pref/suf and return
vector<pair<int, int>> go_end[2];
calc_dp(go_with_ret[0], go_end[0]);
reverse(seq.begin(), seq.end());
calc_dp(go_with_ret[1], go_end[1]);
reverse(seq.begin(), seq.end());
reverse(go_with_ret[1].begin(), go_with_ret[1].end());
reverse(go_end[1].begin(), go_end[1].end());
debug(go_end[0]);
debug(go_end[1]);
// Find some start
FOR (c, 1, N) {
const auto& a = seq[c - 1];
// End left
int k = a - go_with_ret[1][c + 1];
if (1 <= k && k <= a
&& (go_end[0][c - 1].first == k || go_end[0][c - 1].second == k))
{
debug("end left", c, k);
return true;
}
// End right
k = a - go_with_ret[0][c - 1];
if (1 <= k && k <= a
&& (go_end[1][c + 1].first == k || go_end[1][c + 1].second == k))
{
debug("end right", c, k);
return true;
}
// End at start
{
int k1 = go_with_ret[0][c - 1] + 1;
int k2 = a - go_with_ret[1][c + 1];
if (k1 == k2 && 1 <= k1 && k1 <= a) {
debug("end start", c, k1);
return true;
}
}
}
return false;
}
int
main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> T;
while (T--) cout << (solve() ? "TAK\n" : "NIE\n");
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 | // clang-format off #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(auto i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<"("<<p.first<<", "<<p.second<<")";} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";} #ifdef DEBUG #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<"\n";}(X) #else #define debug(...) {} #endif // clang-format on int T, N, max_val; vector<int> seq(N); inline void insert(pair<int, int>& p, int val, int limit) { if (val < 1 || val > limit || val == p.first || val == p.second) return; if (val > p.first) { swap(p.first, p.second); p.first = val; } else if (val > p.second) { p.second = val; } } void calc_dp(vector<int>& go_with_ret, vector<pair<int, int>>& go_end) { go_with_ret.resize(N + 2, -1); go_with_ret[0] = 0; FOR (n, 1, N) { const auto& a = seq[n - 1]; auto& curr = go_with_ret[n]; curr = a - go_with_ret[n - 1]; if (curr < 1 || curr > a) curr = -1; } go_end.resize(N + 2); FOR (n, 1, N) { const auto& a = seq[n - 1]; insert(go_end[n], go_with_ret[n], a); insert(go_end[n], a - go_end[n - 1].first + 1, a); insert(go_end[n], a - go_end[n - 1].second + 1, a); } } bool solve() { cin >> N; seq.resize(N); for (auto& x : seq) cin >> x; // Crop zeros { while (seq.back() == 0) seq.pop_back(); auto l = seq.begin(); while (*l == 0) ++l; seq.assign(l, seq.end()); N = ssize(seq); } // Preprocessing max_val = 0; REP (i, N) { if (seq[i] == 0) return false; max_val = max(max_val, seq[i]); } debug(seq); vector<int> go_with_ret[2]; // # to go to visit whole pref/suf and return vector<pair<int, int>> go_end[2]; calc_dp(go_with_ret[0], go_end[0]); reverse(seq.begin(), seq.end()); calc_dp(go_with_ret[1], go_end[1]); reverse(seq.begin(), seq.end()); reverse(go_with_ret[1].begin(), go_with_ret[1].end()); reverse(go_end[1].begin(), go_end[1].end()); debug(go_end[0]); debug(go_end[1]); // Find some start FOR (c, 1, N) { const auto& a = seq[c - 1]; // End left int k = a - go_with_ret[1][c + 1]; if (1 <= k && k <= a && (go_end[0][c - 1].first == k || go_end[0][c - 1].second == k)) { debug("end left", c, k); return true; } // End right k = a - go_with_ret[0][c - 1]; if (1 <= k && k <= a && (go_end[1][c + 1].first == k || go_end[1][c + 1].second == k)) { debug("end right", c, k); return true; } // End at start { int k1 = go_with_ret[0][c - 1] + 1; int k2 = a - go_with_ret[1][c + 1]; if (k1 == k2 && 1 <= k1 && k1 <= a) { debug("end start", c, k1); return true; } } } return false; } int main() { cin.tie(0)->sync_with_stdio(0); cin >> T; while (T--) cout << (solve() ? "TAK\n" : "NIE\n"); return 0; } |
English