#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void test() {
int n;
cin >> n;
vector<int> toys;
bool leadingZero = true;
int num;
for (int i = 0; i < n; i++) {
cin >> num;
if (num != 0) {
leadingZero = false;
}
if (!leadingZero) {
toys.push_back(num);
}
}
while (!toys.empty() && toys.back() == 0) {
toys.pop_back();
}
n = toys.size();
int i = 0;
bool start = false;
while (i < n) {
if (toys[i] == 0) {
cout << "NIE" << "\n";
return;
}
if (i == n - 1) {
if (toys[i] > 1) {
cout << "NIE" << "\n";
} else {
cout << "TAK" << "\n";
}
return;
}
if (toys[i] > toys[i + 1] + 1) {
cout << "NIE" << "\n";
return;
}
if (toys[i] == toys[i + 1] + 1) {
for (int j = i + 2; j < n; j++) {
if (toys[j] > 0) {
cout << "NIE" << "\n";
return;
}
}
cout << "TAK" << "\n";
return;
}
if (toys[i] == toys[i + 1]) {
i += 2;
start = true;
} else {
if (start) {
toys[i + 1] -= toys[i] - 1;
} else {
toys[i + 1] -= toys[i];
}
i++;
}
}
cout << "TAK" << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
for (int i = 0; i < t; i++) {
test();
}
}
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; void test() { int n; cin >> n; vector<int> toys; bool leadingZero = true; int num; for (int i = 0; i < n; i++) { cin >> num; if (num != 0) { leadingZero = false; } if (!leadingZero) { toys.push_back(num); } } while (!toys.empty() && toys.back() == 0) { toys.pop_back(); } n = toys.size(); int i = 0; bool start = false; while (i < n) { if (toys[i] == 0) { cout << "NIE" << "\n"; return; } if (i == n - 1) { if (toys[i] > 1) { cout << "NIE" << "\n"; } else { cout << "TAK" << "\n"; } return; } if (toys[i] > toys[i + 1] + 1) { cout << "NIE" << "\n"; return; } if (toys[i] == toys[i + 1] + 1) { for (int j = i + 2; j < n; j++) { if (toys[j] > 0) { cout << "NIE" << "\n"; return; } } cout << "TAK" << "\n"; return; } if (toys[i] == toys[i + 1]) { i += 2; start = true; } else { if (start) { toys[i + 1] -= toys[i] - 1; } else { toys[i + 1] -= toys[i]; } i++; } } cout << "TAK" << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; for (int i = 0; i < t; i++) { test(); } } |
English