Cod sursa(job #1631323)
Utilizator | Data | 5 martie 2016 14:55:44 | |
---|---|---|---|
Problema | Perle | Scor | 70 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 6.79 kb |
#include <bits/stdc++.h>
#define for0(i,n) for(int i=0; i<n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define V vector<int>
#define VP vector<pair<int, int> >
#define clr(A,x) memset(A, x, sizeof(A))
#define cpy(A,B) memcpy(A, B, sizeof(B))
#define g(s) getline(fin, s)
//#define for1(i,n) for(int i=1; i<=n; i++)
#define FASTIO ios_base::sync_with_stdio(0)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MAX=100005;
const ull MOD=100003;
const int NR_CIFRE=100005;
/*template <typename T>
string to_string(const T& n){
ostringstream os;
os << n;
return os.str();
}
*/
ifstream fin("perle.in");
ofstream fout("perle.out");
int main()
{
//fout<<"yomama";
deque<short> v;
deque<short> coada; /// 4=A, 5=B, 6=C;
int n;
fin>>n;
for(int i=1; i<=n; i++)
{
v.clear();
coada.clear();
int k;
fin>>k;
for(int j=1; j<=k; j++)
{
short x;
fin>>x;
v.pb(x);
}
if(k==1)
{
fout<<"1\n";
v.clear();
}
else
{
int ans=2;
if(v.front()==1)
{
if(v.size()==3 && v[0]==1 && v[1]==2)
ans=1;//cout<<"1\n";
else
{
if(v.size()<5)
ans=0;//cout<<"0\n";
else
{
if(v[2]!=3)
ans=0;//cout<<"0\n";
else
{
v.pop_front(); v.pop_front(); v.pop_front(); v.pop_front();
coada.push_back(6);
}
}
}
}
else if(v.front()==2)
{
while(!v.empty() && v.front()==2)
v.pop_front();
if(v.empty())
ans=0;//cout<<"0\n";
else
coada.push_back(5);
}
else if(v.front()==3)
{
v.pop_front();
coada.push_back(5);
coada.push_back(6);
}
if(ans!=2)
fout<<ans<<'\n';//<<"oiasdpfais";
else
{
while(!coada.empty())
{ //cout<<coada.front();
if(coada.front()==1)
{
if(!v.empty() && v.front()==1)
{
v.pop_front();
coada.pop_front();
}
else
{
fout<<"0\n";
break;
}
}
if(coada.front()==2)
{
if(!v.empty() && v.front()==2)
{
v.pop_front();
coada.pop_front();
}
else
{
fout<<"0\n";
break;
}
}
if(coada.front()==3)
{
if(!v.empty() && v.front()==3)
{
v.pop_front();
coada.pop_front();
}
else
{
fout<<"0\n";
break;
}
}
if(coada.front()==4)
{
if(!v.empty())
{
coada.pop_front();
v.pop_front();
}
else
{
fout<<"0\n";
break;
}
}
if(coada.front()==5)
{
if(v.empty())
{
fout<<"0\n";
break;
}
if(v.front()==2)
{
coada.pop_front();
coada.push_front(5);
coada.push_front(2);
//cout<<coada.front();
}
else if(v.front()==1)
{
coada.pop_front();
//cout<<9;
coada.push_front(6); coada.push_front(4); coada.push_front(3); coada.push_front(4); coada.push_front(1);
}
else if(v.front()==3)
{
fout<<"0\n";
break;
}
// cout<<coada.front();
}
if(coada.front()==6)
{
if(v.empty())
{
fout<<"0\n";
break;
}
if(v.front()==1)
{
coada.pop_front();
coada.push_front(4); coada.push_front(2); coada.push_front(1);
}
else if(v.front()==2)
{
coada.pop_front();
coada.push_front(2);
}
else if(v.front()==3)
{
coada.pop_front();
coada.push_front(6); coada.push_front(5); coada.push_front(3);
}
}
}
if(coada.empty() && v.empty())
fout<<"1\n";
else
{
if(coada.empty())
{
fout<<"0\n";
v.clear();
}
else
{
coada.clear();
}
}
// cout<<1;
}
}
}
return 0;
}