#include<bits/stdc++.h>
#define pb push_back
#define pob pop_back
#define eb emplace_back
#define fi first
#define se second
#define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mid ((l+r)/2)
#define midLL ((l+r)/2LL)
#define mide ((lo+hi)/2)
#define mideLL ((lo+hi)/2LL)
#define endl '\n'
#define random_shuffle shandom_ruffle
#define lowbit(x) x&(-x)
#define bits(x) __builtin_popcount(x)
#define mins(se) (*se.begin())
#define maxs(se) (*--se.end())
#define all(x) x.begin(), x.end()
#define log2_floor(i) (i ? __builtin_clzll(1) - __builtin_clzll(i) : -1)
#define siz(cont) ((int)cont.size())
#define each(it, cont) for(auto &it : cont)
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using pli = std::pair<ll, int>;
using pil = std::pair<int, ll>;
using vll = std::vector<pll>;
using vii = std::vector<pii>;
using vi = std::vector<int>;
using vl = std::vector<ll>;
using vvl = std::vector<vl>;
using vvi = std::vector<vi>;
using vvii = std::vector<vii>;
using vli = std::vector<pli>;
using vvli = std::vector<vli>;
using vil = std::vector<pil>;
using vvil = std::vector<vil>;
using vc = std::vector<char>;
using vvc = std::vector<std::vector<char>>;
using vb = std::vector<bool>;
using vvb = std::vector<vb>;
void err() { std::cout << "\n"; fflush(stdout); }
template<class T, class... Ts>
void err(T arg, Ts &... args) {
std::cout << arg << ' ';
err(args...);
}
using namespace std;
constexpr bool DEBUG = false;
int n;
void nie(int ile = 0) {
if (DEBUG)
cout << ile << " ";
cout << "NIE\n";
}
void tak(int ile = 0) {
if (DEBUG)
cout << ile << " ";
cout << "TAK\n";
}
void taknie(bool co, int ile = 0) {
if (DEBUG)
cout << ile << " ";
cout << (co ? "TAK\n" : "NIE\n");
}
vi trim(vi v) {
while (!v.back())
v.pop_back();
reverse(all(v));
while(!v.back())
v.pop_back();
reverse(all(v));
return v;
}
vii checker(vi v) {
v[0]--;
for (int i = 1; i < n; i++) {
if (v[i] >= v[i-1]+1) {
v[i] -= v[i-1]+1;
v[i-1] = 0;
}
else {
v[i-1] -= v[i]-1;
v[i] = 0;
}
}
vii res;
for (int i = 0; i < n; i++)
if (v[i]) {
if (v[i] == 1 or v[i] == 2)
res.pb({v[i], i});
else return vii({{-1, 0}});
}
return res;
}
void zamiatara_zero(vi &v, int breaker = -1) {
for (int i = 1; i < n; i++) {
if (v[i] >= v[i-1]+1) {
v[i] -= v[i-1]+1;
v[i-1] = 0;
}
else {
v[i-1] -= v[i]-1;
v[i] = 0;
}
if (i == breaker)
return;
}
}
bool zamiatara_one(vi v, int st) {
for (int i = 0; i <= st; i++)
v[i]--;
zamiatara_zero(v);
for (int i = 0; i < n; i++)
if (v[i]) {
return false;
}
return true;
}
bool zamiatara_two(vi v, int st) {
for (int i = 0; i < n; i++)
v[i]--;
zamiatara_zero(v, st);
reverse(all(v));
zamiatara_zero(v, n-st-1);
for (auto it : v)
if (it)
return false;
return true;
}
bool zamiatara_double(vi v, int a, int b) {
for (int i = 0; i <= a; i++)
v[i]--;
zamiatara_zero(v, b);
for (int i = b+1; i < n; i++)
v[i]--;
reverse(all(v));
zamiatara_zero(v, n-b-1);
for (auto it : v)
if (it)
return false;
return true;
}
void solve() {
cin >> n;
vi v(n);
each(it, v)
cin >> it;
v = trim(v);
n = v.size();
if (n == 1) {
taknie(v[0] < 2, 1);
return;
}
for (auto it : v)
if (!it) {
nie(2);
return;
}
vii res = checker(v);
if (res.empty()) {
tak(3);
return;
}
if (res[0].fi == -1) {
nie(4);
return;
}
if (res.size() == 1) {
if (res[0].fi == 2) {
if (zamiatara_two(v, res[0].se)) {
tak(5);
return;
}
nie(5);
return;
}
if (zamiatara_one(v, res[0].se)) {
tak(6);
return;
}
reverse(all(v));
if (zamiatara_one(v, n-res[0].se-1)) {
tak(7);
return;
}
nie(8);
return;
}
if (res.size() == 2) {
auto [a, xa] = res[0];
auto [b, xb] = res[1];
if (a != 1 or b != 1) {
nie(9);
return;
}
if (zamiatara_double(v, xa, xb)) {
tak(9);
return;
}
nie(9);
return;
}
nie(20);
}
signed main() {
turbo;
int tt; cin >> tt;
while (tt--) {
solve();
}
}
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | #include<bits/stdc++.h> #define pb push_back #define pob pop_back #define eb emplace_back #define fi first #define se second #define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define mid ((l+r)/2) #define midLL ((l+r)/2LL) #define mide ((lo+hi)/2) #define mideLL ((lo+hi)/2LL) #define endl '\n' #define random_shuffle shandom_ruffle #define lowbit(x) x&(-x) #define bits(x) __builtin_popcount(x) #define mins(se) (*se.begin()) #define maxs(se) (*--se.end()) #define all(x) x.begin(), x.end() #define log2_floor(i) (i ? __builtin_clzll(1) - __builtin_clzll(i) : -1) #define siz(cont) ((int)cont.size()) #define each(it, cont) for(auto &it : cont) using ll = long long; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; using pli = std::pair<ll, int>; using pil = std::pair<int, ll>; using vll = std::vector<pll>; using vii = std::vector<pii>; using vi = std::vector<int>; using vl = std::vector<ll>; using vvl = std::vector<vl>; using vvi = std::vector<vi>; using vvii = std::vector<vii>; using vli = std::vector<pli>; using vvli = std::vector<vli>; using vil = std::vector<pil>; using vvil = std::vector<vil>; using vc = std::vector<char>; using vvc = std::vector<std::vector<char>>; using vb = std::vector<bool>; using vvb = std::vector<vb>; void err() { std::cout << "\n"; fflush(stdout); } template<class T, class... Ts> void err(T arg, Ts &... args) { std::cout << arg << ' '; err(args...); } using namespace std; constexpr bool DEBUG = false; int n; void nie(int ile = 0) { if (DEBUG) cout << ile << " "; cout << "NIE\n"; } void tak(int ile = 0) { if (DEBUG) cout << ile << " "; cout << "TAK\n"; } void taknie(bool co, int ile = 0) { if (DEBUG) cout << ile << " "; cout << (co ? "TAK\n" : "NIE\n"); } vi trim(vi v) { while (!v.back()) v.pop_back(); reverse(all(v)); while(!v.back()) v.pop_back(); reverse(all(v)); return v; } vii checker(vi v) { v[0]--; for (int i = 1; i < n; i++) { if (v[i] >= v[i-1]+1) { v[i] -= v[i-1]+1; v[i-1] = 0; } else { v[i-1] -= v[i]-1; v[i] = 0; } } vii res; for (int i = 0; i < n; i++) if (v[i]) { if (v[i] == 1 or v[i] == 2) res.pb({v[i], i}); else return vii({{-1, 0}}); } return res; } void zamiatara_zero(vi &v, int breaker = -1) { for (int i = 1; i < n; i++) { if (v[i] >= v[i-1]+1) { v[i] -= v[i-1]+1; v[i-1] = 0; } else { v[i-1] -= v[i]-1; v[i] = 0; } if (i == breaker) return; } } bool zamiatara_one(vi v, int st) { for (int i = 0; i <= st; i++) v[i]--; zamiatara_zero(v); for (int i = 0; i < n; i++) if (v[i]) { return false; } return true; } bool zamiatara_two(vi v, int st) { for (int i = 0; i < n; i++) v[i]--; zamiatara_zero(v, st); reverse(all(v)); zamiatara_zero(v, n-st-1); for (auto it : v) if (it) return false; return true; } bool zamiatara_double(vi v, int a, int b) { for (int i = 0; i <= a; i++) v[i]--; zamiatara_zero(v, b); for (int i = b+1; i < n; i++) v[i]--; reverse(all(v)); zamiatara_zero(v, n-b-1); for (auto it : v) if (it) return false; return true; } void solve() { cin >> n; vi v(n); each(it, v) cin >> it; v = trim(v); n = v.size(); if (n == 1) { taknie(v[0] < 2, 1); return; } for (auto it : v) if (!it) { nie(2); return; } vii res = checker(v); if (res.empty()) { tak(3); return; } if (res[0].fi == -1) { nie(4); return; } if (res.size() == 1) { if (res[0].fi == 2) { if (zamiatara_two(v, res[0].se)) { tak(5); return; } nie(5); return; } if (zamiatara_one(v, res[0].se)) { tak(6); return; } reverse(all(v)); if (zamiatara_one(v, n-res[0].se-1)) { tak(7); return; } nie(8); return; } if (res.size() == 2) { auto [a, xa] = res[0]; auto [b, xb] = res[1]; if (a != 1 or b != 1) { nie(9); return; } if (zamiatara_double(v, xa, xb)) { tak(9); return; } nie(9); return; } nie(20); } signed main() { turbo; int tt; cin >> tt; while (tt--) { solve(); } } |
English