#include <bits/stdc++.h>
using namespace std;
int s[1000009];
int s2[1000009];
void algo() {
int n;
cin >> n;
int first_not_zero = -1;
int last_not_zero = -1;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
s[i + 1] = x;
s2[i + 1] = x;
if (x != 0) {
if (first_not_zero == -1) {
first_not_zero = i + 1;
}
last_not_zero = i + 1;
}
}
if (first_not_zero == -1) {
cout << "NIE"<< "\n";
return;
}
for (int i = first_not_zero; i <= last_not_zero; i++) {
if (s[i] == 0) {
cout << "NIE"<< "\n";
return;
}
}
int size = last_not_zero - first_not_zero + 1;
if (size == 1) {
if (s[first_not_zero] != 1) {
cout << "NIE"<< "\n";
} else {
cout << "TAK"<< "\n";
}
return;
}
if (size == 2) {
if (abs(s[first_not_zero] - s[last_not_zero]) > 1) {
cout << "NIE"<< "\n";
} else {
cout << "TAK"<< "\n";
}
return;
}
int sum1 = 0;
int sum2 = 0;
for (int i = first_not_zero; i <= last_not_zero; i+=2) {
sum1 += s[i];
}
for (int i = first_not_zero + 1; i <= last_not_zero; i+=2) {
sum2 += s[i];
}
if (abs(sum1 - sum2) >= 2) {
cout << "NIE"<< "\n";
return;
}
int counter = 0;
vector<int> ligma_pos;
for (int i = first_not_zero + 1; i <= last_not_zero; i++) {
s[i] -= s[i - 1];
if (s[i] <= -2) {
cout << "NIE" << "\n";
return;
} else{
if (s[i] == -1) {
counter++;
ligma_pos.push_back(i);
}
}
}
if (counter >= 2) {
cout << "NIE"<< "\n";
return;
}
if (counter == 1) {
if (ligma_pos.back() != last_not_zero) {
cout << "NIE"<< "\n";
return;
}
}
counter = 0;
ligma_pos.clear();
for (int i = last_not_zero - 1; i >= first_not_zero; i--) {
s2[i] -= s2[i + 1];
if (s2[i] <= -2) {
cout << "NIE"<< "\n";
return;
} else if (s2[i] == -1) {
counter++;
ligma_pos.push_back(i);
}
}
if (counter >= 2) {
cout << "NIE"<< "\n";
return;
}
if (counter == 1) {
if (ligma_pos.back() != first_not_zero) {
cout << "NIE"<< "\n";
return;
}
}
cout << "TAK"<< "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
algo();
}
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 132 | #include <bits/stdc++.h> using namespace std; int s[1000009]; int s2[1000009]; void algo() { int n; cin >> n; int first_not_zero = -1; int last_not_zero = -1; for (int i = 0; i < n; i++) { int x; cin >> x; s[i + 1] = x; s2[i + 1] = x; if (x != 0) { if (first_not_zero == -1) { first_not_zero = i + 1; } last_not_zero = i + 1; } } if (first_not_zero == -1) { cout << "NIE"<< "\n"; return; } for (int i = first_not_zero; i <= last_not_zero; i++) { if (s[i] == 0) { cout << "NIE"<< "\n"; return; } } int size = last_not_zero - first_not_zero + 1; if (size == 1) { if (s[first_not_zero] != 1) { cout << "NIE"<< "\n"; } else { cout << "TAK"<< "\n"; } return; } if (size == 2) { if (abs(s[first_not_zero] - s[last_not_zero]) > 1) { cout << "NIE"<< "\n"; } else { cout << "TAK"<< "\n"; } return; } int sum1 = 0; int sum2 = 0; for (int i = first_not_zero; i <= last_not_zero; i+=2) { sum1 += s[i]; } for (int i = first_not_zero + 1; i <= last_not_zero; i+=2) { sum2 += s[i]; } if (abs(sum1 - sum2) >= 2) { cout << "NIE"<< "\n"; return; } int counter = 0; vector<int> ligma_pos; for (int i = first_not_zero + 1; i <= last_not_zero; i++) { s[i] -= s[i - 1]; if (s[i] <= -2) { cout << "NIE" << "\n"; return; } else{ if (s[i] == -1) { counter++; ligma_pos.push_back(i); } } } if (counter >= 2) { cout << "NIE"<< "\n"; return; } if (counter == 1) { if (ligma_pos.back() != last_not_zero) { cout << "NIE"<< "\n"; return; } } counter = 0; ligma_pos.clear(); for (int i = last_not_zero - 1; i >= first_not_zero; i--) { s2[i] -= s2[i + 1]; if (s2[i] <= -2) { cout << "NIE"<< "\n"; return; } else if (s2[i] == -1) { counter++; ligma_pos.push_back(i); } } if (counter >= 2) { cout << "NIE"<< "\n"; return; } if (counter == 1) { if (ligma_pos.back() != first_not_zero) { cout << "NIE"<< "\n"; return; } } cout << "TAK"<< "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { algo(); } return 0; } |
English