#include <bits/stdc++.h>
using namespace std;
int n;
int values[1000001];
int counter = 0;
int idx = 0;
int xorV = 0;
void solve()
{
cin >> n;
for (int i = 0; i < n; ++i) cin >> values[i];
int l = -1, r = -1;
for (int i = 0; i < n; ++i)
{
if (values[i] != 0)
{
if (r != -1)
{
cout << "NIE\n";
return;
}
if (l == -1) l = i;
}
else
{
if (l != -1 && r == -1) r = i - 1;
}
}
if (r == -1) r = n - 1;
bool started = false;
bool ended = false;
for (int i = l; i <= r; ++i)
{
if (ended)
{
if (values[i] != 0)
{
cout << "NIE\n";
return;
}
continue;
}
if (started)
{
if (values[i] == 0)
{
ended = true;
continue;
}
--values[i];
}
if (values[i] == 0)
{
started = true;
}
if (values[i] < 0)
{
cout << "NIE\n";
return;
}
if (i == r)
{
if (values[i] != 0)
{
if (!started) --values[i];
if (values[i] != 0)
{
cout << "NIE\n";
return;
}
}
continue;
}
if (values[i] == values[i + 1] + 1)
{
if (started)
{
cout << "NIE\n";
return;
}
started = true;
--values[i];
}
values[i + 1] -= values[i];
}
cout << "TAK\n";
// ++counter;
// xorV ^= idx;
}
// void generate(vector<int> current)
// {
// if (current.size() == 8)
// {
// for (int i = 0; i < 8; ++i) values[i] = current[i];
// if (idx != 0)
// solve();
// ++idx;
// return;
// }
// current.push_back(0);
// for (int i = 0; i <= 7; ++i)
// {
// current[current.size() - 1] = i;
// generate(current);
// }
// }
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
for (int i = 0; i < t; ++i)
solve();
// n = 8;
// for (int )
// generate({});
// cout << counter << '\n';
// cout << xorV << '\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 125 126 127 128 129 130 131 132 133 | #include <bits/stdc++.h> using namespace std; int n; int values[1000001]; int counter = 0; int idx = 0; int xorV = 0; void solve() { cin >> n; for (int i = 0; i < n; ++i) cin >> values[i]; int l = -1, r = -1; for (int i = 0; i < n; ++i) { if (values[i] != 0) { if (r != -1) { cout << "NIE\n"; return; } if (l == -1) l = i; } else { if (l != -1 && r == -1) r = i - 1; } } if (r == -1) r = n - 1; bool started = false; bool ended = false; for (int i = l; i <= r; ++i) { if (ended) { if (values[i] != 0) { cout << "NIE\n"; return; } continue; } if (started) { if (values[i] == 0) { ended = true; continue; } --values[i]; } if (values[i] == 0) { started = true; } if (values[i] < 0) { cout << "NIE\n"; return; } if (i == r) { if (values[i] != 0) { if (!started) --values[i]; if (values[i] != 0) { cout << "NIE\n"; return; } } continue; } if (values[i] == values[i + 1] + 1) { if (started) { cout << "NIE\n"; return; } started = true; --values[i]; } values[i + 1] -= values[i]; } cout << "TAK\n"; // ++counter; // xorV ^= idx; } // void generate(vector<int> current) // { // if (current.size() == 8) // { // for (int i = 0; i < 8; ++i) values[i] = current[i]; // if (idx != 0) // solve(); // ++idx; // return; // } // current.push_back(0); // for (int i = 0; i <= 7; ++i) // { // current[current.size() - 1] = i; // generate(current); // } // } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; for (int i = 0; i < t; ++i) solve(); // n = 8; // for (int ) // generate({}); // cout << counter << '\n'; // cout << xorV << '\n'; } |
English