#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while(T--) {
int n;
cin >> n;
bool possible = true;
int start = -1, end = -1;
vector<long long> a(n);
long long total = 0;
for(int i = 0; i < n; i++) {
cin >> a[i];
total += a[i];
if((i == 0 && a[i] > 0) || (i > 0 && a[i - 1] == 0 && a[i] > 0)) {
start = i;
}
if((i == n - 1 && a[i] > 0) || (i < n - 1 && a[i + 1] == 0 && a[i] > 0)) {
end = i;
}
}
if(total % 2 == 0) {
cout << "NIE\n";
continue;
}
if(n == 1) {
cout << (a[0] == 1 ? "TAK\n" : "NIE\n");
continue;
}
for(int i = 0; i < start; i++) {
if(a[i] > 0) {
possible = false;
break;
}
}
for(int i = end + 1; i < n; i++) {
if(a[i] > 0) {
possible = false;
break;
}
}
if(!possible) {
cout << "NIE\n";
continue;
}
int len = end - start + 1;
vector<long long> b(len), d(len);
vector<int> cand;
long long max_d = -1;
for(int i = 0; i < len; i++) {
b[i] = 1LL + min(i, len - 1 - i);
d[i] = a[start + i] - b[i];
if(d[i] < 0) {
possible = false;
break;
}
}
if(!possible) {
cout << "NIE\n";
continue;
}
for(int i = 0; i < len; i++) {
max_d = max(max_d, d[i]);
}
for(int i = 0; i < len; i++) {
if(d[i] == max_d) {
cand.push_back(i);
}
}
bool found = false;
for(int c : cand) {
bool is_valid = true;
for(int i = 0; i < len; i++) {
long long expected = max(max_d - (long long)abs(i - c), 0LL);
if(d[i] != expected) {
is_valid = false;
break;
}
}
if(is_valid) {
found = true;
break;
}
}
cout << (found ? "TAK\n" : "NIE\n");
}
}
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 | #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int T; cin >> T; while(T--) { int n; cin >> n; bool possible = true; int start = -1, end = -1; vector<long long> a(n); long long total = 0; for(int i = 0; i < n; i++) { cin >> a[i]; total += a[i]; if((i == 0 && a[i] > 0) || (i > 0 && a[i - 1] == 0 && a[i] > 0)) { start = i; } if((i == n - 1 && a[i] > 0) || (i < n - 1 && a[i + 1] == 0 && a[i] > 0)) { end = i; } } if(total % 2 == 0) { cout << "NIE\n"; continue; } if(n == 1) { cout << (a[0] == 1 ? "TAK\n" : "NIE\n"); continue; } for(int i = 0; i < start; i++) { if(a[i] > 0) { possible = false; break; } } for(int i = end + 1; i < n; i++) { if(a[i] > 0) { possible = false; break; } } if(!possible) { cout << "NIE\n"; continue; } int len = end - start + 1; vector<long long> b(len), d(len); vector<int> cand; long long max_d = -1; for(int i = 0; i < len; i++) { b[i] = 1LL + min(i, len - 1 - i); d[i] = a[start + i] - b[i]; if(d[i] < 0) { possible = false; break; } } if(!possible) { cout << "NIE\n"; continue; } for(int i = 0; i < len; i++) { max_d = max(max_d, d[i]); } for(int i = 0; i < len; i++) { if(d[i] == max_d) { cand.push_back(i); } } bool found = false; for(int c : cand) { bool is_valid = true; for(int i = 0; i < len; i++) { long long expected = max(max_d - (long long)abs(i - c), 0LL); if(d[i] != expected) { is_valid = false; break; } } if(is_valid) { found = true; break; } } cout << (found ? "TAK\n" : "NIE\n"); } } |
English