#include <iostream> using namespace std; using ll = long long; int count[1001000]; int cp[1001000]; void makeCopy(ll n) { for(int i = 0; i < n; i++) { cp[i] = count[i]; } } bool tryCover(ll n) { for(int i = 0; i < n - 1; i++) { if (cp[i]) { if (cp[i + 1] < cp[i]) return false; cp[i + 1] -= cp[i]; cp[i] = 0; } } return !cp[n - 1]; } void test() { ll n; cin >> n; ll start = -1; ll end = n + 1; for(int i = 0;i < n; i++) { cin >> count[i]; if (start == -1 && count[i]) { start = i; } if (count[i]) { end = i; } } if (start == -1) { cout << "NIE\n"; return; } if (start == end) { if (count[start] == 1) { cout << "TAK\n"; return; } else { cout << "NIE\n"; return; } } for(int i = start + 1; i < end; i++) { if (!count[i]) { cout << "NIE\n"; return; } count[i]--; } bool result = false; makeCopy(n); if (tryCover(n)) { result = true; } else { makeCopy(n); cp[start] --; if (tryCover(n)) { result = true; } else { makeCopy(n); cp[end]--; if (tryCover(n)) { result = true; } else { makeCopy(n); cp[start]--; cp[end]--; if (tryCover(n)) { result = true; } } } } if (result) { cout << "TAK\n"; } else { cout << "NIE\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t-->0) { test(); } }
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 | #include <iostream> using namespace std; using ll = long long; int count[1001000]; int cp[1001000]; void makeCopy(ll n) { for(int i = 0; i < n; i++) { cp[i] = count[i]; } } bool tryCover(ll n) { for(int i = 0; i < n - 1; i++) { if (cp[i]) { if (cp[i + 1] < cp[i]) return false; cp[i + 1] -= cp[i]; cp[i] = 0; } } return !cp[n - 1]; } void test() { ll n; cin >> n; ll start = -1; ll end = n + 1; for(int i = 0;i < n; i++) { cin >> count[i]; if (start == -1 && count[i]) { start = i; } if (count[i]) { end = i; } } if (start == -1) { cout << "NIE\n"; return; } if (start == end) { if (count[start] == 1) { cout << "TAK\n"; return; } else { cout << "NIE\n"; return; } } for(int i = start + 1; i < end; i++) { if (!count[i]) { cout << "NIE\n"; return; } count[i]--; } bool result = false; makeCopy(n); if (tryCover(n)) { result = true; } else { makeCopy(n); cp[start] --; if (tryCover(n)) { result = true; } else { makeCopy(n); cp[end]--; if (tryCover(n)) { result = true; } else { makeCopy(n); cp[start]--; cp[end]--; if (tryCover(n)) { result = true; } } } } if (result) { cout << "TAK\n"; } else { cout << "NIE\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t-->0) { test(); } } |