Pagini recente » Cod sursa (job #1392138) | Cod sursa (job #3125873) | Cod sursa (job #639650) | Cod sursa (job #1704993) | Cod sursa (job #189647)
Cod sursa(job #189647)
#include <stdio.h>
#include <math.h>
long n, l, a[10001];
long test_b(int i);
long test_c(int i);
long test_b(int i) {
for (; i < l; ++i) {
if (a[i] != 2) {
break;
}
}
if (i == l) {
return 0;
}
if (l < i + 5) {
return 0;
}
if (a[i] != 1 || a[i + 2] != 3) {
return 0;
}
return test_c(i + 4);
}
long test_c(int i) {
if (a[i] == 2) {
return i + 1;
}
if (a[i] == 1) {
if (l < i + 3) {
return 0;
}
if (a[i + 1] == 2) {
return i + 3;
}
}
i = test_b(i + 1);
if (i == l || i == 0) {
return 0;
}
return test_c(i);
}
long test() {
if (l == 1) {
return 1;
}
if (a[0] == 1) {
if (l == 3 && a[1] == 2) {
return 1;
}
if (l >= 3 && a[2] == 3) {
return (test_c(4) == l);
}
}
if (a[0] == 2) {
return (test_b(0) == l);
}
return (test_c(0) == l);
}
int main()
{
int i,j;
freopen("perle.in","r",stdin);
freopen("perle.out","w",stdout);
scanf("%ld", &n);
for (i = 0;i < n; ++i) {
scanf("%ld", &l);
for (j = 0; j < l; ++j) {
scanf("%ld", &a[j]);
}
printf("%ld\n", test());
}
return 0;
}