#include <cstdio> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <list> #include <vector> #include <map> #include <unordered_map> #include <unordered_set> #include <set> #include <bitset> #include <stack> #include <string> #include <cstring> #include <cassert> #include <limits.h> using namespace std; typedef unsigned long long LL; typedef long double LD; typedef vector<int> VI; #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(VAR(i,a);i<=(b);++i) #define FORD(i,a,b) for(VAR(i,a);i>=(b);--i) #define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a) #define VAR(a,b) __typeof(b) a=(b) #define SIZE(a) ((int)((a).size())) #define ALL(x) (x).begin(),(x).end() #define CLR(x,a) memset(x,a,sizeof(x)) #define ZERO(x) memset(x,0,sizeof(x)) #define S(t,i) scanf("%" ## t, &i) #define SI(i) scanf("%d", &i) const int MAXN=1000000+11; int w[MAXN], w1[MAXN]; bool solve(int si, int ei, bool oneway=false) { int starti=si; while (si+1 < ei) { if (w[si] == 0) { return false; } w[si]--; if (w[si+1] == w[si]) { w[si+1] = 1; w[si] = 0; if (si+1 < ei-1 || oneway) { return false; } } else if (w[si+1] < w[si]){ return false; } else { w[si+1] -= w[si]; w[si] = 0; } si++; } return w[si] == 1; } bool solve2(int si, int ei) { int onepoint=false; int breakpoint1 = -1; while (si+1 < ei) { if (w[si+1] == w[si]) { breakpoint1 = si; break; } else if (w[si+1] + 1 == w[si]){ breakpoint1 = si; onepoint = true; break; } else if (w[si+1] < w[si]) { return false; } else { w[si+1] -= w[si]; w[si] = 0; } si++; } if (breakpoint1 == -1 || breakpoint1 == 0 || breakpoint1 == ei-1) { return false; } int breakpoint2 = breakpoint1; si=ei-1; while (si > breakpoint1) { if (w[si-1] == w[si]) { breakpoint2 = si; if (onepoint && breakpoint1 != breakpoint2) { return false; } break; } else if (w[si-1] < w[si]) { return false; } else { w[si-1] -= w[si]; w[si] = 0; } si--; } return solve(breakpoint1, breakpoint2+1, true); } int main() { ios_base::sync_with_stdio(0); int t; cin >> t; REP(i, t) { int n; cin >> n; int si = 0; bool started = false; bool ended = false; bool niespojny = false; REP(ni, n) { int a; cin >> a; if (a > 0) { if (!started) { started = true; } else if (ended) { niespojny = true; } w[si] = a; w1[si] = w[si]; si++; } else { if (started) { ended = true; } } } if (niespojny) { cout << "NIE\n"; } else { if (solve(0, si)) { cout << "TAK\n"; } else { // cout << "NIE\n"; FOR(wi, 0, si-1) { w[si-1-wi] = w1[wi]; } if (solve(0, si)) { cout << "TAK\n"; } else { FOR(wi, 0, si-1) { w[wi] = w1[wi]; } if (solve2(0, si)) { cout << "TAK\n"; } else { cout << "NIE\n"; } } } } } 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 162 163 164 | #include <cstdio> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <list> #include <vector> #include <map> #include <unordered_map> #include <unordered_set> #include <set> #include <bitset> #include <stack> #include <string> #include <cstring> #include <cassert> #include <limits.h> using namespace std; typedef unsigned long long LL; typedef long double LD; typedef vector<int> VI; #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(VAR(i,a);i<=(b);++i) #define FORD(i,a,b) for(VAR(i,a);i>=(b);--i) #define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a) #define VAR(a,b) __typeof(b) a=(b) #define SIZE(a) ((int)((a).size())) #define ALL(x) (x).begin(),(x).end() #define CLR(x,a) memset(x,a,sizeof(x)) #define ZERO(x) memset(x,0,sizeof(x)) #define S(t,i) scanf("%" ## t, &i) #define SI(i) scanf("%d", &i) const int MAXN=1000000+11; int w[MAXN], w1[MAXN]; bool solve(int si, int ei, bool oneway=false) { int starti=si; while (si+1 < ei) { if (w[si] == 0) { return false; } w[si]--; if (w[si+1] == w[si]) { w[si+1] = 1; w[si] = 0; if (si+1 < ei-1 || oneway) { return false; } } else if (w[si+1] < w[si]){ return false; } else { w[si+1] -= w[si]; w[si] = 0; } si++; } return w[si] == 1; } bool solve2(int si, int ei) { int onepoint=false; int breakpoint1 = -1; while (si+1 < ei) { if (w[si+1] == w[si]) { breakpoint1 = si; break; } else if (w[si+1] + 1 == w[si]){ breakpoint1 = si; onepoint = true; break; } else if (w[si+1] < w[si]) { return false; } else { w[si+1] -= w[si]; w[si] = 0; } si++; } if (breakpoint1 == -1 || breakpoint1 == 0 || breakpoint1 == ei-1) { return false; } int breakpoint2 = breakpoint1; si=ei-1; while (si > breakpoint1) { if (w[si-1] == w[si]) { breakpoint2 = si; if (onepoint && breakpoint1 != breakpoint2) { return false; } break; } else if (w[si-1] < w[si]) { return false; } else { w[si-1] -= w[si]; w[si] = 0; } si--; } return solve(breakpoint1, breakpoint2+1, true); } int main() { ios_base::sync_with_stdio(0); int t; cin >> t; REP(i, t) { int n; cin >> n; int si = 0; bool started = false; bool ended = false; bool niespojny = false; REP(ni, n) { int a; cin >> a; if (a > 0) { if (!started) { started = true; } else if (ended) { niespojny = true; } w[si] = a; w1[si] = w[si]; si++; } else { if (started) { ended = true; } } } if (niespojny) { cout << "NIE\n"; } else { if (solve(0, si)) { cout << "TAK\n"; } else { // cout << "NIE\n"; FOR(wi, 0, si-1) { w[si-1-wi] = w1[wi]; } if (solve(0, si)) { cout << "TAK\n"; } else { FOR(wi, 0, si-1) { w[wi] = w1[wi]; } if (solve2(0, si)) { cout << "TAK\n"; } else { cout << "NIE\n"; } } } } } return 0; } |