#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define all(a) begin(a), end(a)
using ll = long long;
bool solve(vector<ll> a) {
while (a.back() == 0)
a.pop_back();
reverse(all(a));
while (a.back() == 0)
a.pop_back();
int n = a.size();
if (count(all(a), 0))
return false;
ll tval = 0;
for (auto i : a)
tval = i - tval;
if (abs(tval) > 1)
return false;
vector<ll> pref = a, suff = a;
for (int i = 0; i + 1 < n; i++)
pref[i + 1] -= pref[i];
for (int i = n - 1; i > 0; i--)
suff[i - 1] -= suff[i];
for (int st = 0; st < 2; st++) {
int bad = 0;
for (int i = 0; i < st; i++)
bad += pref[i] <= 0;
for (int i = st; i < n; i++)
bad += suff[i] <= 0;
for (int fin = st; fin < n; fin++) {
bad -= suff[fin] <= 0;
ll val = pref[fin] + suff[fin] - a[fin];
if (!bad && val == !((fin - st) % 2))
return true;
bad += pref[fin] < 0;
if (fin % 2 != (st + 1) % 2)
bad += pref[fin] == 0;
}
}
return false;
}
void solve() {
int n;
cin >> n;
vector<ll> a(n);
for (auto &i : a)
cin >> i;
cout << (solve(a) ? "TAK\n" : "NIE\n");
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int tests = 1;
cin >> tests;
while (tests--)
solve();
}
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 | #include <algorithm> #include <bits/stdc++.h> using namespace std; #define all(a) begin(a), end(a) using ll = long long; bool solve(vector<ll> a) { while (a.back() == 0) a.pop_back(); reverse(all(a)); while (a.back() == 0) a.pop_back(); int n = a.size(); if (count(all(a), 0)) return false; ll tval = 0; for (auto i : a) tval = i - tval; if (abs(tval) > 1) return false; vector<ll> pref = a, suff = a; for (int i = 0; i + 1 < n; i++) pref[i + 1] -= pref[i]; for (int i = n - 1; i > 0; i--) suff[i - 1] -= suff[i]; for (int st = 0; st < 2; st++) { int bad = 0; for (int i = 0; i < st; i++) bad += pref[i] <= 0; for (int i = st; i < n; i++) bad += suff[i] <= 0; for (int fin = st; fin < n; fin++) { bad -= suff[fin] <= 0; ll val = pref[fin] + suff[fin] - a[fin]; if (!bad && val == !((fin - st) % 2)) return true; bad += pref[fin] < 0; if (fin % 2 != (st + 1) % 2) bad += pref[fin] == 0; } } return false; } void solve() { int n; cin >> n; vector<ll> a(n); for (auto &i : a) cin >> i; cout << (solve(a) ? "TAK\n" : "NIE\n"); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int tests = 1; cin >> tests; while (tests--) solve(); } |
English