#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto&operator <<(auto& o, pair<auto, auto> p) {return o<<"{"<<p.first<<", "<<p.second<<"}";}
auto operator <<(auto& o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";}
#define debug(X...) cerr<<"["#X"]: ", [](auto...$) {((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
#define int long long
const int INF = 1e18+7;
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define eb emplace_back
bool check(vector<int> a)
{
while(a.size() && a.back() == 0)
a.pop_back();
if(a.size() == 0) return true;
reverse(a.begin(), a.end());
while(a.size() && a.back() == 0)
a.pop_back();
if(a.size() == 0) return true;
reverse(a.begin(), a.end());
int akt = 0;
for(int i=0;i<a.size();i++)
{
if(a[i] == 0) return false;
int d = a[i] - 1;
if(akt <= d) {akt = d-akt; continue;}
if(i == a.size()-1 && akt <= d+1) return true;
akt = -1;
break;
}
if(akt == 0)
return true;
if(a.size() >= 2)
{
if(a[1] > a[0])
{
akt = a[1] - a[0] - 1;
for(int i=2;i<a.size();i++)
{
if(a[i] == 0) return false;
int d = a[i] - 1;
if(akt <= d) {akt = d-akt; continue;}
if(i == a.size()-1 && akt <= d+1) return true;
return false;
}
if(akt == 0)
return true;
}
}
return false;
}
void solve()
{
int n;
cin>>n;
vector<int> a(n);
for(auto& v : a) cin>>v;
if(check(a)) {cout<<"TAK\n"; return;}
reverse(a.begin(), a.end());
if(check(a)) {cout<<"TAK\n"; return;}
cout<<"NIE\n";
}
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
int t;
cin>>t;
while(t--) solve();
}
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 | #include<bits/stdc++.h> using namespace std; #ifdef DEBUG auto&operator <<(auto& o, pair<auto, auto> p) {return o<<"{"<<p.first<<", "<<p.second<<"}";} auto operator <<(auto& o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";} #define debug(X...) cerr<<"["#X"]: ", [](auto...$) {((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif #define int long long const int INF = 1e18+7; #define mp(x, y) make_pair(x, y) #define fi first #define se second #define eb emplace_back bool check(vector<int> a) { while(a.size() && a.back() == 0) a.pop_back(); if(a.size() == 0) return true; reverse(a.begin(), a.end()); while(a.size() && a.back() == 0) a.pop_back(); if(a.size() == 0) return true; reverse(a.begin(), a.end()); int akt = 0; for(int i=0;i<a.size();i++) { if(a[i] == 0) return false; int d = a[i] - 1; if(akt <= d) {akt = d-akt; continue;} if(i == a.size()-1 && akt <= d+1) return true; akt = -1; break; } if(akt == 0) return true; if(a.size() >= 2) { if(a[1] > a[0]) { akt = a[1] - a[0] - 1; for(int i=2;i<a.size();i++) { if(a[i] == 0) return false; int d = a[i] - 1; if(akt <= d) {akt = d-akt; continue;} if(i == a.size()-1 && akt <= d+1) return true; return false; } if(akt == 0) return true; } } return false; } void solve() { int n; cin>>n; vector<int> a(n); for(auto& v : a) cin>>v; if(check(a)) {cout<<"TAK\n"; return;} reverse(a.begin(), a.end()); if(check(a)) {cout<<"TAK\n"; return;} cout<<"NIE\n"; } int32_t main() { cin.tie(0)->sync_with_stdio(0); int t; cin>>t; while(t--) solve(); } |
English