#include <cstdio> using namespace std; #define MAX 1100000 int a[MAX],t,n; int reduce(int i, int dx) { while(a[i]<a[i+dx]) { a[i+dx]-=a[i]; a[i]=0; i+=dx; } return i; } bool oblicz() { int pos1=1,pos2=n; while(pos1<=n && a[pos1]==0) pos1++; while(pos2>=1 && a[pos2]==0) pos2--; pos1 = reduce(pos1,+1); pos2 = reduce(pos2,-1); if(pos1 == pos2) return a[pos1] == 1; for(int i = pos1;i<pos2;i++) a[i]++; pos1 = reduce(pos1,+1); pos2 = reduce(pos2,-1); if(pos1 == pos2) return a[pos1] == 1; return false; } int main() { scanf("%d", &t); while(t--) { scanf("%d", &n); a[n+1] = a[0] = 0; for(int i=1;i<=n;i++) { scanf("%d", &a[i]); } printf((oblicz()?"TAK\n": "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 | #include <cstdio> using namespace std; #define MAX 1100000 int a[MAX],t,n; int reduce(int i, int dx) { while(a[i]<a[i+dx]) { a[i+dx]-=a[i]; a[i]=0; i+=dx; } return i; } bool oblicz() { int pos1=1,pos2=n; while(pos1<=n && a[pos1]==0) pos1++; while(pos2>=1 && a[pos2]==0) pos2--; pos1 = reduce(pos1,+1); pos2 = reduce(pos2,-1); if(pos1 == pos2) return a[pos1] == 1; for(int i = pos1;i<pos2;i++) a[i]++; pos1 = reduce(pos1,+1); pos2 = reduce(pos2,-1); if(pos1 == pos2) return a[pos1] == 1; return false; } int main() { scanf("%d", &t); while(t--) { scanf("%d", &n); a[n+1] = a[0] = 0; for(int i=1;i<=n;i++) { scanf("%d", &a[i]); } printf((oblicz()?"TAK\n": "NIE\n")); } } |