#include<bits/stdc++.h>
#define llong long long
#define ldouble long double
#define uint unsigned int
#define ulong unsigned long long
using namespace std;
bool test_smooth(int i, int j, vector<int> &a) {
while (j - i > 1) {
if (a[i] <= a[i + 1]) {
a[i + 1] -= a[i] - 1;
i++;
continue;
}
if (a[j] <= a[j - 1]) {
a[j - 1] -= a[j] - 1;
j--;
continue;
}
return false;
}
if (i == j) return a[i] == 1;
return a[i] == a[j];
}
bool test_normal(int i, int j, vector<int> &a) {
while (j - i > 1) {
if (a[i] < a[i + 1]) {
a[i + 1] -= a[i];
i++;
continue;
}
if (a[j] < a[j - 1]) {
a[j - 1] -= a[j];
j--;
continue;
}
if (a[i] > a[i + 1]) return false;
if (a[j] > a[j - 1]) return false;
return test_smooth(i + 2, j, a);
}
if (i == j) return a[i] <= 1;
return abs(a[i] - a[j]) <= 1;
}
void solve() {
int n; cin >> n;
vector<int> a(n);
for (int &ai : a) cin >> ai;
int i = 0, j = n - 1;
bool smooth = false;
while (i < n && a[i] == 0) i++;
while (j >= 0 && a[j] == 0) j--;
if (i > j) {
cout << "TAK\n";
return;
}
for (int k = i; k <= j; k++) {
if (a[k] == 0) {
cout << "NIE\n";
return;
}
}
if (test_normal(i, j, a)) cout << "TAK\n";
else cout << "NIE\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t; cin >> t;
while (t--) solve();
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 | #include<bits/stdc++.h> #define llong long long #define ldouble long double #define uint unsigned int #define ulong unsigned long long using namespace std; bool test_smooth(int i, int j, vector<int> &a) { while (j - i > 1) { if (a[i] <= a[i + 1]) { a[i + 1] -= a[i] - 1; i++; continue; } if (a[j] <= a[j - 1]) { a[j - 1] -= a[j] - 1; j--; continue; } return false; } if (i == j) return a[i] == 1; return a[i] == a[j]; } bool test_normal(int i, int j, vector<int> &a) { while (j - i > 1) { if (a[i] < a[i + 1]) { a[i + 1] -= a[i]; i++; continue; } if (a[j] < a[j - 1]) { a[j - 1] -= a[j]; j--; continue; } if (a[i] > a[i + 1]) return false; if (a[j] > a[j - 1]) return false; return test_smooth(i + 2, j, a); } if (i == j) return a[i] <= 1; return abs(a[i] - a[j]) <= 1; } void solve() { int n; cin >> n; vector<int> a(n); for (int &ai : a) cin >> ai; int i = 0, j = n - 1; bool smooth = false; while (i < n && a[i] == 0) i++; while (j >= 0 && a[j] == 0) j--; if (i > j) { cout << "TAK\n"; return; } for (int k = i; k <= j; k++) { if (a[k] == 0) { cout << "NIE\n"; return; } } if (test_normal(i, j, a)) cout << "TAK\n"; else cout << "NIE\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; while (t--) solve(); return 0; } |
English