#include <iostream> #include <vector> #include <set> #include <map> #include <algorithm> #include <unordered_map> #include <unordered_set> #include <queue> #include <deque> #include <tuple> #include <bitset> using namespace std; #define pb push_back #define mp make_pair #define st first #define nd second #define x first #define y second typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--){ int n; cin >> n; vi A; int first_non_zero = -1; int last_non_zero = -1; for(int i=0; i<n; i++){ int a; cin >> a; A.pb(a); if(a!=0){ last_non_zero = i; if(first_non_zero == -1){ first_non_zero = i; } } } bool f = true; // Czy blok jest ciągły for(int i=first_non_zero; i<last_non_zero; i++){ if(A[i]==0){ f=false; break; } } if(!f){ cout << "NIE\n"; continue; } // Pojedynczy element if(first_non_zero==last_non_zero){ if(A[first_non_zero]==1){ cout << "TAK\n"; }else{ cout << "NIE\n"; } continue; } int k=A[first_non_zero]-1; // Left to right starts 0 for(int i=first_non_zero+1; i<=last_non_zero; i++){ k=(A[i]-1)-k; // cout << "k " << k << " i " << i << "\n"; if((k<0 && i!= last_non_zero)){ f = false; break; } } if((k==0||k==-1) && f){ cout << "TAK\n"; continue; } k=A[first_non_zero]; f=true; // Left to right starts 1 for(int i=first_non_zero+1; i<=last_non_zero; i++){ k=(A[i]-1)-k; // cout << "k " << k << " i " << i << "\n"; if((k<0 && i!= last_non_zero)){ f = false; break; } } if((k==0||k==-1) && f){ cout << "TAK\n"; continue; } cout << "NIE\n"; } 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 | #include <iostream> #include <vector> #include <set> #include <map> #include <algorithm> #include <unordered_map> #include <unordered_set> #include <queue> #include <deque> #include <tuple> #include <bitset> using namespace std; #define pb push_back #define mp make_pair #define st first #define nd second #define x first #define y second typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--){ int n; cin >> n; vi A; int first_non_zero = -1; int last_non_zero = -1; for(int i=0; i<n; i++){ int a; cin >> a; A.pb(a); if(a!=0){ last_non_zero = i; if(first_non_zero == -1){ first_non_zero = i; } } } bool f = true; // Czy blok jest ciągły for(int i=first_non_zero; i<last_non_zero; i++){ if(A[i]==0){ f=false; break; } } if(!f){ cout << "NIE\n"; continue; } // Pojedynczy element if(first_non_zero==last_non_zero){ if(A[first_non_zero]==1){ cout << "TAK\n"; }else{ cout << "NIE\n"; } continue; } int k=A[first_non_zero]-1; // Left to right starts 0 for(int i=first_non_zero+1; i<=last_non_zero; i++){ k=(A[i]-1)-k; // cout << "k " << k << " i " << i << "\n"; if((k<0 && i!= last_non_zero)){ f = false; break; } } if((k==0||k==-1) && f){ cout << "TAK\n"; continue; } k=A[first_non_zero]; f=true; // Left to right starts 1 for(int i=first_non_zero+1; i<=last_non_zero; i++){ k=(A[i]-1)-k; // cout << "k " << k << " i " << i << "\n"; if((k<0 && i!= last_non_zero)){ f = false; break; } } if((k==0||k==-1) && f){ cout << "TAK\n"; continue; } cout << "NIE\n"; } return 0; } |