#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
int q, n, tab[1000006], tab_pom[1000006];
bool check(int x, int (&tab)[]) {
for(int i = x ; i < n ; i++) {
if(tab[i] == tab[i + 1]) {
tab[i] = min(tab[i], 0);
tab[i + 1] = min(tab[i + 1], 0);
break;
}
else if(tab[i] > tab[i + 1] || tab[i] < 0 || tab[i + 1] < 0) {
return false;
}
tab[i + 1] -= tab[i] + 1;
tab[i] = 0;
}
for(int i = 0 ; i < n ; i++) {
if(tab[i] != 0) {
return false;
}
}
return true;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> q;
while(q--) {
cin >> n;
int first_pos = -1;
for(int i = 0; i < n; i++) {
cin >> tab[i];
tab_pom[i] = tab[i];
if(first_pos == -1 && tab[i] > 0) {
first_pos = i;
}
}
tab_pom[first_pos]--;
bool chk1 = check(first_pos, tab_pom);
bool chk2 = check(first_pos, tab);
cout << (chk1 || chk2 ? "TAK" : "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 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second int q, n, tab[1000006], tab_pom[1000006]; bool check(int x, int (&tab)[]) { for(int i = x ; i < n ; i++) { if(tab[i] == tab[i + 1]) { tab[i] = min(tab[i], 0); tab[i + 1] = min(tab[i + 1], 0); break; } else if(tab[i] > tab[i + 1] || tab[i] < 0 || tab[i + 1] < 0) { return false; } tab[i + 1] -= tab[i] + 1; tab[i] = 0; } for(int i = 0 ; i < n ; i++) { if(tab[i] != 0) { return false; } } return true; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> q; while(q--) { cin >> n; int first_pos = -1; for(int i = 0; i < n; i++) { cin >> tab[i]; tab_pom[i] = tab[i]; if(first_pos == -1 && tab[i] > 0) { first_pos = i; } } tab_pom[first_pos]--; bool chk1 = check(first_pos, tab_pom); bool chk2 = check(first_pos, tab); cout << (chk1 || chk2 ? "TAK" : "NIE") << "\n"; } } |
English