#include <iostream>
#include <set>
#include <utility>
#include <vector>
#include <tuple>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
using state = tuple<int, pair<bool, bool>, bool>;
int t;
cin >> t;
for (int j = 0; j < t; j++) {
int n;
cin >> n;
vector<int> in(n, 0);
for (int i = 0; i < n; i++) {
cin >> in[i];
}
set<state> states;
set<state> new_states;
int a = in[0];
if (n == 1) {
if (a == 1) {
cout << "TAK\n";
} else {
cout << "NIE\n";
}
continue;
}
bool good = true;
bool nonzero = false;
bool zero = false;
for (int i = 0; i < n; i++) {
if (!nonzero && in[i] != 0) {
nonzero = false;
} else if (nonzero && !zero && in[i] == 0) {
zero = true;
} else if (nonzero && zero && in[i] != 0) {
good = false;
break;
}
}
if (!good) {
cout << "NIE\n";
break;
}
states.insert(make_tuple(a, std::pair<bool, bool> { false, false }, a != 0));
if (a > 0) {
states.insert(make_tuple ( a, std::pair<bool, bool> { true, false }, true ));
states.insert(make_tuple ( a - 1, std::pair<bool, bool> { false, true }, true ));
states.insert(make_tuple ( a - 1, std::pair<bool, bool> { true, true }, true ));
}
for (int i = 1; i < n; i++) {
if (states.empty()) {
good = false;
break;
}
a = in[i];
for (auto [prev, s, was] : states) {
auto [b, e] = s;
if (prev == 0 && was) {
if (a == 0) {
new_states.insert(make_tuple(prev, s, was));
} else {
continue;
}
} else if (prev == 0 && a == 0 && !was) {
new_states.insert(make_tuple(prev, s, was));
} else {
if (a >= prev) {
if (b == false && e == false) {
new_states.insert(make_tuple(a - prev, std::pair<bool, bool> { false, false }, true));
if (a - prev - 1 >= 0) {
new_states.insert(make_tuple(a - prev - 1, std::pair<bool, bool> { false, true }, true));
new_states.insert(make_tuple(a - prev - 1, std::pair<bool, bool> { true, true }, true));
}
new_states.insert(make_tuple(a - prev, std::pair<bool, bool> { true, false }, true));
} else if (b == true && e == false) {
new_states.insert(make_tuple(a - prev + 1, std::pair<bool, bool> { true, false }, true));
new_states.insert(make_tuple(a - prev, std::pair<bool, bool> { true, true }, true));
} else if (b == false && e == true) {
new_states.insert(make_tuple(a - prev, std::pair<bool, bool> { false, true }, true));
new_states.insert(make_tuple(a - prev, std::pair<bool, bool> { true, true }, true));
} else {
new_states.insert(make_tuple(a - prev, std::pair<bool, bool> { true, true }, true));
}
}
}
}
states = std::move(new_states);
new_states.clear();
}
if (good) {
if (states.count(make_tuple(0, std::pair<bool, bool> { true, true }, true)) > 0) {
cout << "TAK\n";
} else {
cout << "NIE\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 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 | #include <iostream> #include <set> #include <utility> #include <vector> #include <tuple> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); using state = tuple<int, pair<bool, bool>, bool>; int t; cin >> t; for (int j = 0; j < t; j++) { int n; cin >> n; vector<int> in(n, 0); for (int i = 0; i < n; i++) { cin >> in[i]; } set<state> states; set<state> new_states; int a = in[0]; if (n == 1) { if (a == 1) { cout << "TAK\n"; } else { cout << "NIE\n"; } continue; } bool good = true; bool nonzero = false; bool zero = false; for (int i = 0; i < n; i++) { if (!nonzero && in[i] != 0) { nonzero = false; } else if (nonzero && !zero && in[i] == 0) { zero = true; } else if (nonzero && zero && in[i] != 0) { good = false; break; } } if (!good) { cout << "NIE\n"; break; } states.insert(make_tuple(a, std::pair<bool, bool> { false, false }, a != 0)); if (a > 0) { states.insert(make_tuple ( a, std::pair<bool, bool> { true, false }, true )); states.insert(make_tuple ( a - 1, std::pair<bool, bool> { false, true }, true )); states.insert(make_tuple ( a - 1, std::pair<bool, bool> { true, true }, true )); } for (int i = 1; i < n; i++) { if (states.empty()) { good = false; break; } a = in[i]; for (auto [prev, s, was] : states) { auto [b, e] = s; if (prev == 0 && was) { if (a == 0) { new_states.insert(make_tuple(prev, s, was)); } else { continue; } } else if (prev == 0 && a == 0 && !was) { new_states.insert(make_tuple(prev, s, was)); } else { if (a >= prev) { if (b == false && e == false) { new_states.insert(make_tuple(a - prev, std::pair<bool, bool> { false, false }, true)); if (a - prev - 1 >= 0) { new_states.insert(make_tuple(a - prev - 1, std::pair<bool, bool> { false, true }, true)); new_states.insert(make_tuple(a - prev - 1, std::pair<bool, bool> { true, true }, true)); } new_states.insert(make_tuple(a - prev, std::pair<bool, bool> { true, false }, true)); } else if (b == true && e == false) { new_states.insert(make_tuple(a - prev + 1, std::pair<bool, bool> { true, false }, true)); new_states.insert(make_tuple(a - prev, std::pair<bool, bool> { true, true }, true)); } else if (b == false && e == true) { new_states.insert(make_tuple(a - prev, std::pair<bool, bool> { false, true }, true)); new_states.insert(make_tuple(a - prev, std::pair<bool, bool> { true, true }, true)); } else { new_states.insert(make_tuple(a - prev, std::pair<bool, bool> { true, true }, true)); } } } } states = std::move(new_states); new_states.clear(); } if (good) { if (states.count(make_tuple(0, std::pair<bool, bool> { true, true }, true)) > 0) { cout << "TAK\n"; } else { cout << "NIE\n"; } } else { cout << "NIE\n"; } } } |
English