Cod sursa(job #2311650)
Utilizator | Data | 3 ianuarie 2019 15:56:48 | |
---|---|---|---|
Problema | Perle | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva de probleme | Marime | 2.44 kb |
#include <cstdio>
#define NMAX 10000
int st[2][NMAX+10]; // 1 2 3 4(A) 5(B) 6(C)
int niv[2];
int valid[2];
int main()
{
int t,l,v,i;
freopen("perle.in","r",stdin);
freopen("perle.out","w",stdout);
scanf("%d",&t);
while (t--)
{
scanf("%d",&l);
if (l == 1)
{
scanf("%d",&v);
printf("1\n");
continue;
}
else
{
valid[1] = valid[0] = 1;
niv[1] = niv[0] = 1;
st[0][0] = 5;
st[1][0] = 6;
while (l--)
{
scanf("%d",&v);
for (i = 0; i < 2; i++)
{
if (valid[i])
{
if (st[i][niv[i]-1] < 4)
{
valid[i] = (st[i][--niv[i]]==v);
continue;
}
if (st[i][niv[i]-1] == 4)
{
niv[i]--;
continue;
}
if (st[i][niv[i]-1]==5)
{
if (v == 3) valid[i] = 0;
if (v == 1)
{
niv[i]--;
st[i][niv[i]++] = 6;
st[i][niv[i]++] = 4;
st[i][niv[i]++] = 3;
st[i][niv[i]++] = 4;
}
continue;
}
if (v == 2) niv[i]--;
if (v == 1)
{
niv[i]--;
st[i][niv[i]++] = 4;
st[i][niv[i]++] = 2;
}
if (v == 3)
{
niv[i]--;
st[i][niv[i]++] = 6;
st[i][niv[i]++] = 5;
}
}
if (!niv[i] && l) valid[i] = 0;
}
}
printf("%d\n",((!niv[0] && valid[0]) || (!niv[1] && valid[1])));
}
}
return 0;
}