#include <bits/stdc++.h>
using namespace std;
int calc_range(vector <int> &a, int &l, bool &both) {
int n = (int) a.size();
l = a[0] - 1; // ile zostalo do sparowania na poprzedniej pozycji
both = 1; // czy oba konce sciezki sa na koncu
for(int i = 1; i < n; ++i) {
int x = a[i];
if(both) {
if(x == 1 || x - 2 < l) {
both = 0;
if(x - 1 < l) {
both = 1;
return i - 1;
}
}
else {
x -= 2 + l;
l = x;
}
}
if(!both) {
if(x - 1 < l) return i - 1;
else {
x -= 1 + l;
l = x;
}
}
}
return n - 1;
}
void solve() {
int n; cin >> n;
vector <int> a(n);
for(int &x : a) cin >> x;
// wywalenie zer (odwrocenie nie ma znaczenia)
while(a.back() == 0) a.pop_back();
reverse(a.begin(), a.end());
while(a.back() == 0) a.pop_back();
n = (int) a.size();
int l;
bool bl;
int pos = calc_range(a, l, bl);
a[pos] = 0;
//cout << "pos l bl: " << pos << " " << l << " " << bl << "\n";
if(pos == n - 1) {
if(l == 0) cout << "TAK\n";
else cout << "NIE\n";
return;
}
reverse(a.begin(), a.end());
int r;
bool br;
//calc_range(a, r, br);
//cout << "posr r br: " << n - 1 - calc_range(a, r, br) << " " << r << " " << br << "\n";
if(n - 1 - calc_range(a, r, br) != pos + 1) {
cout << "NIE\n";
return;
}
if((l == r) || (l == r + 1 && br) || (r == l + 1 && bl)) cout << "TAK\n";
else cout << "NIE\n";
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int t; cin >> t;
for(++t; --t;) solve();
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 | #include <bits/stdc++.h> using namespace std; int calc_range(vector <int> &a, int &l, bool &both) { int n = (int) a.size(); l = a[0] - 1; // ile zostalo do sparowania na poprzedniej pozycji both = 1; // czy oba konce sciezki sa na koncu for(int i = 1; i < n; ++i) { int x = a[i]; if(both) { if(x == 1 || x - 2 < l) { both = 0; if(x - 1 < l) { both = 1; return i - 1; } } else { x -= 2 + l; l = x; } } if(!both) { if(x - 1 < l) return i - 1; else { x -= 1 + l; l = x; } } } return n - 1; } void solve() { int n; cin >> n; vector <int> a(n); for(int &x : a) cin >> x; // wywalenie zer (odwrocenie nie ma znaczenia) while(a.back() == 0) a.pop_back(); reverse(a.begin(), a.end()); while(a.back() == 0) a.pop_back(); n = (int) a.size(); int l; bool bl; int pos = calc_range(a, l, bl); a[pos] = 0; //cout << "pos l bl: " << pos << " " << l << " " << bl << "\n"; if(pos == n - 1) { if(l == 0) cout << "TAK\n"; else cout << "NIE\n"; return; } reverse(a.begin(), a.end()); int r; bool br; //calc_range(a, r, br); //cout << "posr r br: " << n - 1 - calc_range(a, r, br) << " " << r << " " << br << "\n"; if(n - 1 - calc_range(a, r, br) != pos + 1) { cout << "NIE\n"; return; } if((l == r) || (l == r + 1 && br) || (r == l + 1 && bl)) cout << "TAK\n"; else cout << "NIE\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; for(++t; --t;) solve(); return 0; } |
English