#include "iostream" #include "algorithm" #include "vector" #include "utility" bool solve(int index, std::vector<int>& count, bool front) { if (index >= count.size()) { return true; } if (index == count.size() - 1) { return count[index] == 1; } if (count[index] == 0) { return false; } if (front) { if (count[index] > count[index + 1]) { return (index == count.size() - 2 && count[index] == count[index + 1] + 1); } if (count[index] == count[index + 1]) { return solve(index + 2, count, front); } count[index + 1] -= count[index]; count[index + 1] += 1; return solve(index + 1, count, front); } else { if (count[index] > count[index + 1]) { return (index == count.size() - 2 && count[index] == count[index + 1] + 1); } if (count[index] == count[index + 1]) { return solve(index + 2, count, true); } count[index + 1] -= count[index]; count[index + 1] += 1; bool a = solve(index + 1, count, true); count[index + 1] -= 1; bool b = solve(index + 1, count, true); return a || b; } } int main() { int t; std::cin >> t; for (int i = 0; i < t; i++) { int n; std::cin >> n; std::vector<int> count(n); for (int j = 0; j < n; j++) { std::cin >> count[j]; } while (count[count.size() - 1] == 0) { count.pop_back(); } reverse(count.begin(), count.end()); while (count[count.size() - 1] == 0) { count.pop_back(); } reverse(count.begin(), count.end()); std::cout << (solve(0, count, false) ? "TAK" : "NIE") << std::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 | #include "iostream" #include "algorithm" #include "vector" #include "utility" bool solve(int index, std::vector<int>& count, bool front) { if (index >= count.size()) { return true; } if (index == count.size() - 1) { return count[index] == 1; } if (count[index] == 0) { return false; } if (front) { if (count[index] > count[index + 1]) { return (index == count.size() - 2 && count[index] == count[index + 1] + 1); } if (count[index] == count[index + 1]) { return solve(index + 2, count, front); } count[index + 1] -= count[index]; count[index + 1] += 1; return solve(index + 1, count, front); } else { if (count[index] > count[index + 1]) { return (index == count.size() - 2 && count[index] == count[index + 1] + 1); } if (count[index] == count[index + 1]) { return solve(index + 2, count, true); } count[index + 1] -= count[index]; count[index + 1] += 1; bool a = solve(index + 1, count, true); count[index + 1] -= 1; bool b = solve(index + 1, count, true); return a || b; } } int main() { int t; std::cin >> t; for (int i = 0; i < t; i++) { int n; std::cin >> n; std::vector<int> count(n); for (int j = 0; j < n; j++) { std::cin >> count[j]; } while (count[count.size() - 1] == 0) { count.pop_back(); } reverse(count.begin(), count.end()); while (count[count.size() - 1] == 0) { count.pop_back(); } reverse(count.begin(), count.end()); std::cout << (solve(0, count, false) ? "TAK" : "NIE") << std::endl; } return 0; } |