#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("O0")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
//using namespace __gnu_pbds;
#define pb push_back
#define F first
#define S second
#define ll long long
#define ld double long
#define ull unsigned long long
#define endl '\n'
const ll N = 3e5 + 36;
const ll M = 2e3 + 36;
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;
const ll MOD1 = 888888901;
const ll MOD2 = 999988901;
const ll X[8] = {1, -1, 2, 2, -2, -2, 1, -1};
const ll Y[8] = {2, 2, 1, -1, 1, -1, -2, -2};
const ld PI = 3.14159265358979323846;
const ld EPS = 1e-10;
//tree < pair < string, int >, null_type, less < pair < string, int > >, rb_tree_tag, tree_order_statistics_node_update > a;
//mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
mt19937 gen(19937);
inline bool solve(const vector<int>& a, int n) {
for (int i(0); i < 2; ++i) {
vector<int> b = a;
bool now = true;
if (b[i] == 0) {
continue;
}
--b[i];
int lst = -1;
for (int j(i - 1); j >= 0; --j) {
if (b[j] == 0) {
lst = j + 1;
break;
}
--b[j];
}
if (lst == -1) {
lst = 0;
}
for (int j(lst + 1); j < n; ++j) {
//for (int i(0); i < n; ++i) {
//cout << b[i] << ' ';
//}
//cout << endl;
if (b[j] <= 0 || b[j - 1] < 0) {
break;
}
b[j] -= b[j - 1] + 1;
b[j - 1] = 0;
}
int k = 0;
for (int j(0); j < n; ++j) {
//cout << b[j] << ' ';
if (b[j] == -1) {
if (k == 0) {
++k;
} else {
now = false;
}
} else if (b[j] != 0) {
now = false;
}
}
//cout << endl;// << endl;
if (now) {
return true;
}
}
return false;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n, 0);
int x = 0;
bool possible = true;
bool all = true;
int st = -1;
int fin = -1;
for (int i(0); i < n; ++i) {
cin >> a[i];
if (a[i] != 0) {
if (st == -1) {
st = i;
}
fin = i;
if (x == 2) {
possible = false;
}
if (x == 0) {
x = 1;
}
} else {
if (x == 1) {
x = 2;
}
}
if (a[i] != 0) {
all = false;
}
}
vector<int> b;
for (int i(st); i <= fin; ++i){
b.pb(a[i]);
}
a = b;
n = fin - st + 1;
if (!possible) {
cout << "NIE" << endl;
continue;
}
bool ans = false;
if (all) {
ans = true;
}
if (ans == 0) {
ans = solve(a, n);
}
if (ans == 0) {
reverse(a.begin(), a.end());
ans = solve(a, n);
}
if (ans) {
cout << "TAK" << endl;
} else {
cout << "NIE" << endl;
}
}
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | #include <bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //#pragma GCC optimize("O0") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native") #pragma GCC optimize("unroll-loops") using namespace std; //using namespace __gnu_pbds; #define pb push_back #define F first #define S second #define ll long long #define ld double long #define ull unsigned long long #define endl '\n' const ll N = 3e5 + 36; const ll M = 2e3 + 36; const ll INF = 1e9 + 7; const ll MOD = 1e9 + 7; const ll MOD1 = 888888901; const ll MOD2 = 999988901; const ll X[8] = {1, -1, 2, 2, -2, -2, 1, -1}; const ll Y[8] = {2, 2, 1, -1, 1, -1, -2, -2}; const ld PI = 3.14159265358979323846; const ld EPS = 1e-10; //tree < pair < string, int >, null_type, less < pair < string, int > >, rb_tree_tag, tree_order_statistics_node_update > a; //mt19937 gen(chrono::system_clock::now().time_since_epoch().count()); mt19937 gen(19937); inline bool solve(const vector<int>& a, int n) { for (int i(0); i < 2; ++i) { vector<int> b = a; bool now = true; if (b[i] == 0) { continue; } --b[i]; int lst = -1; for (int j(i - 1); j >= 0; --j) { if (b[j] == 0) { lst = j + 1; break; } --b[j]; } if (lst == -1) { lst = 0; } for (int j(lst + 1); j < n; ++j) { //for (int i(0); i < n; ++i) { //cout << b[i] << ' '; //} //cout << endl; if (b[j] <= 0 || b[j - 1] < 0) { break; } b[j] -= b[j - 1] + 1; b[j - 1] = 0; } int k = 0; for (int j(0); j < n; ++j) { //cout << b[j] << ' '; if (b[j] == -1) { if (k == 0) { ++k; } else { now = false; } } else if (b[j] != 0) { now = false; } } //cout << endl;// << endl; if (now) { return true; } } return false; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #else // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif // LOCAL int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n, 0); int x = 0; bool possible = true; bool all = true; int st = -1; int fin = -1; for (int i(0); i < n; ++i) { cin >> a[i]; if (a[i] != 0) { if (st == -1) { st = i; } fin = i; if (x == 2) { possible = false; } if (x == 0) { x = 1; } } else { if (x == 1) { x = 2; } } if (a[i] != 0) { all = false; } } vector<int> b; for (int i(st); i <= fin; ++i){ b.pb(a[i]); } a = b; n = fin - st + 1; if (!possible) { cout << "NIE" << endl; continue; } bool ans = false; if (all) { ans = true; } if (ans == 0) { ans = solve(a, n); } if (ans == 0) { reverse(a.begin(), a.end()); ans = solve(a, n); } if (ans) { cout << "TAK" << endl; } else { cout << "NIE" << endl; } } return 0; } |
English