#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i= 0; i<(n); i++) #define reps(i,s, n) for(int i= (s); i<(n); i++) #define each(a, x) for (auto &a : x) #define vv(T) vector<T> #define endl '\n' #define sz(x) (int)x.size() #define ll long long #define all(c) begin(c), end(c) #define fi first #define se second #define mp make_pair #define pb push_back #define pd pair<int,int> #define wr cout<< #define wre wr endl; #define wrut(a) {each(i,(a))wr i<<" "; wre} #define wrot(a,b,c) {wre wr a<<" "<<b<<" "<<c<<" ";wre} #define int ll const int inf =1e14+7; bool comp(pd p1,pd p2){ return p1.se<p2.se; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin>>t; rep(T,t){ int n; cin>>n; int start=inf,end=-1; vv(int) a(n); rep(i,n){ cin>>a[i]; if(a[i]>0){ end=max(i,end); start=min(start,i); } } if(n==1){ if(a[0]<=1)wr "TAK\n"; else wr "NIE\n"; continue; } int l=start+1,r=end-1; int x=a[start]-1; if(start+1<n){ a[start]-=x; a[start+1]-=x; } x=a[end]-1; if(end-1>=0){ a[end]-=x; a[end-1]-=x; } while(l<end-1&&a[l]>2){ x=a[l]-2; a[l]-=x; a[l+1]-=x; l++; } while(r>start+1&&a[r]>2){ x=a[r]-2; a[r]-=x; a[r-1]-=x; r--; } while(l<n-1&&a[l]>1){ l++; } while(r>0&&a[r]>1){ r--; } if(end-start+1==3&&a[start+1]==3){ wr "TAK\n"; continue; } // wrut(a); reps(i,l+1,r){ if(a[i]>1){ x=a[i]-1; a[i]-=x; a[i+1]-=x; } } bool czy=1; reps(i,start,end+1){ if(a[i]<=0){ czy=0; } } // wrut(a); if(czy&&a[start]==1&&a[end]==1){ wr "TAK\n"; }else{ wr "NIE\n"; } } }
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 | #include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i= 0; i<(n); i++) #define reps(i,s, n) for(int i= (s); i<(n); i++) #define each(a, x) for (auto &a : x) #define vv(T) vector<T> #define endl '\n' #define sz(x) (int)x.size() #define ll long long #define all(c) begin(c), end(c) #define fi first #define se second #define mp make_pair #define pb push_back #define pd pair<int,int> #define wr cout<< #define wre wr endl; #define wrut(a) {each(i,(a))wr i<<" "; wre} #define wrot(a,b,c) {wre wr a<<" "<<b<<" "<<c<<" ";wre} #define int ll const int inf =1e14+7; bool comp(pd p1,pd p2){ return p1.se<p2.se; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin>>t; rep(T,t){ int n; cin>>n; int start=inf,end=-1; vv(int) a(n); rep(i,n){ cin>>a[i]; if(a[i]>0){ end=max(i,end); start=min(start,i); } } if(n==1){ if(a[0]<=1)wr "TAK\n"; else wr "NIE\n"; continue; } int l=start+1,r=end-1; int x=a[start]-1; if(start+1<n){ a[start]-=x; a[start+1]-=x; } x=a[end]-1; if(end-1>=0){ a[end]-=x; a[end-1]-=x; } while(l<end-1&&a[l]>2){ x=a[l]-2; a[l]-=x; a[l+1]-=x; l++; } while(r>start+1&&a[r]>2){ x=a[r]-2; a[r]-=x; a[r-1]-=x; r--; } while(l<n-1&&a[l]>1){ l++; } while(r>0&&a[r]>1){ r--; } if(end-start+1==3&&a[start+1]==3){ wr "TAK\n"; continue; } // wrut(a); reps(i,l+1,r){ if(a[i]>1){ x=a[i]-1; a[i]-=x; a[i+1]-=x; } } bool czy=1; reps(i,start,end+1){ if(a[i]<=0){ czy=0; } } // wrut(a); if(czy&&a[start]==1&&a[end]==1){ wr "TAK\n"; }else{ wr "NIE\n"; } } } |