#include <algorithm> #include <cstdio> #include <tuple> #include <vector> using namespace std; using range = pair<int, int>; using count_type = long long; using number_type = count_type; void print_vector(vector<count_type> v) { for (number_type i = 0; i < v.size(); i++) { printf("%lld ", v[i]); } puts(""); } vector<count_type> strip_vector_from_0(vector<count_type> &v) { number_type first = -1, last = v.size(); for (number_type i = 0; i < v.size(); i++) { if (first == -1 && v[i] > 0) { first = i; } if (v[i] > 0) { last = i + 1; } } return vector<count_type>(v.begin() + first, v.begin() + last); } bool has_zeros(vector<count_type> &v) { for (number_type i = 0; i < v.size(); i++) { if (v[i] == 0) { return true; } } return false; } vector<range> get_paths(count_type odd_sum, count_type even_sum, vector<count_type> &v) { count_type diff = odd_sum - even_sum; number_type last_index = v.size() - 1; bool is_size_odd = last_index % 2; if (diff == 1) { return {{1, is_size_odd ? last_index : last_index - 1}}; } if (diff == -1) { return {{0, is_size_odd ? last_index - 1 : last_index}}; } return { {0, is_size_odd ? last_index : last_index - 1}, {1, is_size_odd ? last_index - 1 : last_index}, }; } bool solve_by_counts_and_path(vector<count_type> counts, range path) { if (path.first) { counts[path.first]--; } if (path.second != counts.size() - 1) { counts[path.second]--; } counts = strip_vector_from_0(counts); if (has_zeros(counts)) { return false; } for (number_type i = 0; i < counts.size() - 1; i++) { counts[i + 1] -= counts[i] - 1; counts[i] = 0; // print_vector(counts); if (counts[i + 1] <= 0) { return false; } } return true; } bool solve(vector<count_type> &counts) { counts = strip_vector_from_0(counts); if (counts.size() == 1) { return counts[0] == 1; } if (has_zeros(counts)) { return false; } // print_vector(counts); count_type odd_sum = 0, even_sum = 0; for (number_type i = 0; i < counts.size(); i++) { if (i % 2) { odd_sum += counts[i]; } else { even_sum += counts[i]; } } // printf("odd: %lld even: %lld\n", odd_sum, even_sum); if (abs(odd_sum - even_sum) > 1) { return false; } vector<range> paths = get_paths(odd_sum, even_sum, counts); for (number_type i = 0; i < paths.size(); i++) { // if (paths.size() > 1) { // printf("path: %d %d\n", paths[i].first, paths[i].second); // print_vector(counts); // } if (solve_by_counts_and_path(counts, paths[i])) { return true; } } return false; } int main() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { int n; scanf("%d", &n); vector<count_type> counts(n); for (int j = 0; j < n; j++) { scanf("%lld", &counts[j]); } if (solve(counts)) { puts("TAK"); } else { puts("NIE"); } } 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | #include <algorithm> #include <cstdio> #include <tuple> #include <vector> using namespace std; using range = pair<int, int>; using count_type = long long; using number_type = count_type; void print_vector(vector<count_type> v) { for (number_type i = 0; i < v.size(); i++) { printf("%lld ", v[i]); } puts(""); } vector<count_type> strip_vector_from_0(vector<count_type> &v) { number_type first = -1, last = v.size(); for (number_type i = 0; i < v.size(); i++) { if (first == -1 && v[i] > 0) { first = i; } if (v[i] > 0) { last = i + 1; } } return vector<count_type>(v.begin() + first, v.begin() + last); } bool has_zeros(vector<count_type> &v) { for (number_type i = 0; i < v.size(); i++) { if (v[i] == 0) { return true; } } return false; } vector<range> get_paths(count_type odd_sum, count_type even_sum, vector<count_type> &v) { count_type diff = odd_sum - even_sum; number_type last_index = v.size() - 1; bool is_size_odd = last_index % 2; if (diff == 1) { return {{1, is_size_odd ? last_index : last_index - 1}}; } if (diff == -1) { return {{0, is_size_odd ? last_index - 1 : last_index}}; } return { {0, is_size_odd ? last_index : last_index - 1}, {1, is_size_odd ? last_index - 1 : last_index}, }; } bool solve_by_counts_and_path(vector<count_type> counts, range path) { if (path.first) { counts[path.first]--; } if (path.second != counts.size() - 1) { counts[path.second]--; } counts = strip_vector_from_0(counts); if (has_zeros(counts)) { return false; } for (number_type i = 0; i < counts.size() - 1; i++) { counts[i + 1] -= counts[i] - 1; counts[i] = 0; // print_vector(counts); if (counts[i + 1] <= 0) { return false; } } return true; } bool solve(vector<count_type> &counts) { counts = strip_vector_from_0(counts); if (counts.size() == 1) { return counts[0] == 1; } if (has_zeros(counts)) { return false; } // print_vector(counts); count_type odd_sum = 0, even_sum = 0; for (number_type i = 0; i < counts.size(); i++) { if (i % 2) { odd_sum += counts[i]; } else { even_sum += counts[i]; } } // printf("odd: %lld even: %lld\n", odd_sum, even_sum); if (abs(odd_sum - even_sum) > 1) { return false; } vector<range> paths = get_paths(odd_sum, even_sum, counts); for (number_type i = 0; i < paths.size(); i++) { // if (paths.size() > 1) { // printf("path: %d %d\n", paths[i].first, paths[i].second); // print_vector(counts); // } if (solve_by_counts_and_path(counts, paths[i])) { return true; } } return false; } int main() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { int n; scanf("%d", &n); vector<count_type> counts(n); for (int j = 0; j < n; j++) { scanf("%lld", &counts[j]); } if (solve(counts)) { puts("TAK"); } else { puts("NIE"); } } return 0; } |