#include <bits/stdc++.h> using namespace std; #ifdef GGDEBUG #define dbg printf #else #define dbg // dbg #endif int n; bool is_continuous(const vector<int> &count) { int position = -1; for (int i = 0; i < count.size(); i++) { if (count[i] > 0) { position = i; break; } } while (position < count.size() && count[position] > 0) { position++; } if (position < count.size()) { assert(count[position] == 0); while (position < count.size() && count[position] == 0) { position++; } if (position < count.size()) { return false; } } return true; } bool is_possible(const vector<int> &c) { if (n == 0) { return true; } for (int start = 0; start < n; ++start) { for (int end = 0; end < n; ++end) { if (c[start] > 0 && c[end] > 0) { vector<int> deg(n); for (int i = 0; i < n; i++) { deg[i] = 2 * c[i]; } deg[start]--; deg[end]--; dbg("deg:"); for (int x : deg) { dbg(" %d", x); } dbg("\n"); bool bad = false; vector<int> edge(n - 1); for (int i = 0; i < n - 1; ++i) { int e = min(deg[i], deg[i + 1]); dbg("edge[%d] = %d\n", i, e); edge[i] = e; deg[i] -= e; deg[i + 1] -= e; } for (int x : deg) { if (x != 0) { bad = true; break; } } if (bad) { continue; } for (int i = 0; i < n - 1; i++) { if (c[i] > 0 && c[i + 1] > 0 && edge[i] == 0) { bad = true; break; } } if (!bad) { // dbg("Valid %d -> %d!\n", start, end); // // print input // for (int i = 0; i < n; i++) { // dbg("%d ", c[i]); // } // dbg("\n"); // // print edges // for (int i = 0; i < n - 1; i++) { // dbg("%d ", edge[i]); // } // dbg("\n"); return true; } } } } return false; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d", &n); vector<int> a; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); a.push_back(x); } if (is_continuous(a) && is_possible(a)) { printf("TAK\n"); } else { printf("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 112 113 114 115 116 117 118 119 120 121 122 123 124 | #include <bits/stdc++.h> using namespace std; #ifdef GGDEBUG #define dbg printf #else #define dbg // dbg #endif int n; bool is_continuous(const vector<int> &count) { int position = -1; for (int i = 0; i < count.size(); i++) { if (count[i] > 0) { position = i; break; } } while (position < count.size() && count[position] > 0) { position++; } if (position < count.size()) { assert(count[position] == 0); while (position < count.size() && count[position] == 0) { position++; } if (position < count.size()) { return false; } } return true; } bool is_possible(const vector<int> &c) { if (n == 0) { return true; } for (int start = 0; start < n; ++start) { for (int end = 0; end < n; ++end) { if (c[start] > 0 && c[end] > 0) { vector<int> deg(n); for (int i = 0; i < n; i++) { deg[i] = 2 * c[i]; } deg[start]--; deg[end]--; dbg("deg:"); for (int x : deg) { dbg(" %d", x); } dbg("\n"); bool bad = false; vector<int> edge(n - 1); for (int i = 0; i < n - 1; ++i) { int e = min(deg[i], deg[i + 1]); dbg("edge[%d] = %d\n", i, e); edge[i] = e; deg[i] -= e; deg[i + 1] -= e; } for (int x : deg) { if (x != 0) { bad = true; break; } } if (bad) { continue; } for (int i = 0; i < n - 1; i++) { if (c[i] > 0 && c[i + 1] > 0 && edge[i] == 0) { bad = true; break; } } if (!bad) { // dbg("Valid %d -> %d!\n", start, end); // // print input // for (int i = 0; i < n; i++) { // dbg("%d ", c[i]); // } // dbg("\n"); // // print edges // for (int i = 0; i < n - 1; i++) { // dbg("%d ", edge[i]); // } // dbg("\n"); return true; } } } } return false; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d", &n); vector<int> a; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); a.push_back(x); } if (is_continuous(a) && is_possible(a)) { printf("TAK\n"); } else { printf("NIE\n"); } } } |