#include <iostream>
using namespace std;
int tab[1000007], tab2[1000007], n, fir, las;
bool wyl(int node) {
while(node != las) {
tab[node]--;
if(tab[node] < 0) return false;
if(tab[node] == 0) {
node++;
continue;
}
else {
tab[node + 1] -= (tab[node]);
tab[node] = 0;
node++;
}
}
if(tab[node] == 1 || tab[node] == 0) return true;
else return false;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--) {
cin>>n;
bool w1 = false, w2 = false;
for(int i = 0; i < n; i++) {
cin>>tab[i];
tab2[i] = tab[i];
}
for(int i = 0; i < n; i++) if(tab[i] != 0) {
fir = i;
break;
}
for(int i = n - 1; i >= 0; i--) if(tab[i] != 0) {
las = i;
break;
}
if(fir == las) {
if(tab[fir] == 1) cout<<"TAK\n";
else cout<<"NIE\n";
continue;
}
w1 = wyl(fir);
for(int i = 0; i < n; i++) tab[i] = tab2[i];
tab[fir + 1] -= tab[fir];
tab[fir] = 0;
w2 = wyl(fir + 1);
if(w1||w2) cout<<"TAK\n";
else cout<<"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 | #include <iostream> using namespace std; int tab[1000007], tab2[1000007], n, fir, las; bool wyl(int node) { while(node != las) { tab[node]--; if(tab[node] < 0) return false; if(tab[node] == 0) { node++; continue; } else { tab[node + 1] -= (tab[node]); tab[node] = 0; node++; } } if(tab[node] == 1 || tab[node] == 0) return true; else return false; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin>>t; while(t--) { cin>>n; bool w1 = false, w2 = false; for(int i = 0; i < n; i++) { cin>>tab[i]; tab2[i] = tab[i]; } for(int i = 0; i < n; i++) if(tab[i] != 0) { fir = i; break; } for(int i = n - 1; i >= 0; i--) if(tab[i] != 0) { las = i; break; } if(fir == las) { if(tab[fir] == 1) cout<<"TAK\n"; else cout<<"NIE\n"; continue; } w1 = wyl(fir); for(int i = 0; i < n; i++) tab[i] = tab2[i]; tab[fir + 1] -= tab[fir]; tab[fir] = 0; w2 = wyl(fir + 1); if(w1||w2) cout<<"TAK\n"; else cout<<"NIE\n"; } } |
English