#include <bits/stdc++.h> using namespace std; set<pair<int, int>> mozliwosci[1000007]; int a[1000007]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int q; cin >> q; while(q--) { int n, l, r; bool czyok = true; l = r = -1; cin >> n; for(int i = 1; i <= n; i++) { cin >> a[i]; mozliwosci[i].clear(); } for(int i = 1; i <= n; i++) { if(a[i] != 0) { l = i; i = n + 7; } } for(int i = n ; i >= 1; i--) { if(a[i] != 0) { r = i; i = -2; } } for(int i = max(l, 1); i <= r; i++) { if(a[i] == 0) { czyok = false; } } if(l == -1 || !czyok) { cout << "NIE" << endl; } else { mozliwosci[l - 1].insert({-1, 0}); for(int i = l; i <= r; i++) { for(auto x : mozliwosci[i - 1]) { if(x.second == 0) { mozliwosci[i].insert({a[i] - x.first - 2, 0}); mozliwosci[i].insert({a[i] - x.first - 2, 1}); mozliwosci[i].insert({a[i] - x.first - 2, 2}); } else if(x.second == 1) { mozliwosci[i].insert({a[i] - x.first - 1, 1}); mozliwosci[i].insert({a[i] - x.first - 1, 2}); } else { if(x.first > 0) { mozliwosci[i].insert({a[i] - x.first, 2}); } } } /*cout << i << endl; for(auto x : mozliwosci[i]) { cout << x.first << " " << x.second << endl; } cout << endl;*/ vector<pair<int, int>> temp; for(auto x : mozliwosci[i]) { if(x.first < 0) { temp.push_back(x); } } for(auto x : temp) { mozliwosci[i].erase(x); } /*if(mozliwosci[i].size() > 9) { cout << "nie" << endl; }*/ temp.clear(); /*cout << i << endl; for(auto x : mozliwosci[i]) { cout << x.first << " " << x.second << endl; } cout << endl;*/ } if(mozliwosci[r].find({0, 2}) != mozliwosci[r].end() && czyok) { cout << "TAK" << endl; } else { cout << "NIE" << endl; } } } 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 114 115 116 117 118 | #include <bits/stdc++.h> using namespace std; set<pair<int, int>> mozliwosci[1000007]; int a[1000007]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int q; cin >> q; while(q--) { int n, l, r; bool czyok = true; l = r = -1; cin >> n; for(int i = 1; i <= n; i++) { cin >> a[i]; mozliwosci[i].clear(); } for(int i = 1; i <= n; i++) { if(a[i] != 0) { l = i; i = n + 7; } } for(int i = n ; i >= 1; i--) { if(a[i] != 0) { r = i; i = -2; } } for(int i = max(l, 1); i <= r; i++) { if(a[i] == 0) { czyok = false; } } if(l == -1 || !czyok) { cout << "NIE" << endl; } else { mozliwosci[l - 1].insert({-1, 0}); for(int i = l; i <= r; i++) { for(auto x : mozliwosci[i - 1]) { if(x.second == 0) { mozliwosci[i].insert({a[i] - x.first - 2, 0}); mozliwosci[i].insert({a[i] - x.first - 2, 1}); mozliwosci[i].insert({a[i] - x.first - 2, 2}); } else if(x.second == 1) { mozliwosci[i].insert({a[i] - x.first - 1, 1}); mozliwosci[i].insert({a[i] - x.first - 1, 2}); } else { if(x.first > 0) { mozliwosci[i].insert({a[i] - x.first, 2}); } } } /*cout << i << endl; for(auto x : mozliwosci[i]) { cout << x.first << " " << x.second << endl; } cout << endl;*/ vector<pair<int, int>> temp; for(auto x : mozliwosci[i]) { if(x.first < 0) { temp.push_back(x); } } for(auto x : temp) { mozliwosci[i].erase(x); } /*if(mozliwosci[i].size() > 9) { cout << "nie" << endl; }*/ temp.clear(); /*cout << i << endl; for(auto x : mozliwosci[i]) { cout << x.first << " " << x.second << endl; } cout << endl;*/ } if(mozliwosci[r].find({0, 2}) != mozliwosci[r].end() && czyok) { cout << "TAK" << endl; } else { cout << "NIE" << endl; } } } return 0; } |