跳到正文

剪贴板

剪贴板 wui9129q

原文
剪贴板信息

剪贴板信息

收录
更新
C++
const int Mx=1<<8;
vector<par>pre[2][8];
inline void Pre(int x)
{//预处理
	int i;
	pre[0][0].epb(mkp(1,1));
	pre[0][0].epb(mkp(0,1));
	pre[0][0].epb(mkp(0,0));
	pre[0][0].epb(mkp(1,0));
	for(par to:pre[0][0])pre[1][0].epb(to);
	reverse(pre[1][0].begin(),pre[1][0].end());
	//一个点的直接手动处理
	//pre[0]:正着走 pre[1]:倒着走
	for(i=1;i<x;i++)
	{//按题解中所说归纳构造
		pre[0][i].epb(mkp(i+1,1));
		for(par to:pre[0][i-1])
		{
			pre[0][i].epb(to);
		}//先正着走
		pre[0][i].epb(mkp(i,1));
		pre[0][i].epb(i+1,1);
		pre[0][i].epb(i+1,0);
		//把当前点包住
		for(par to:pre[1][i-1])
		{
			pre[0][i].epb(mkp(f(to),s(to)));
		}//再倒着原路返回
		pre[0][i].epb(mkp(i,0));
		pre[0][i].epb(mkp(i+1,0));
		for(par to:pre[0][i])pre[1][i].epb(to);
		reverse(pre[1][i].begin(),pre[1][i].end());
		//处理倒着走的方案
	}
}
int tot;
const int M=250000;
par ans[M+5];
inline void Prt(int x,int y,int z)
{//从 (x,z) 跨过中间点走到 (y,z)
	if(x==y)return;
	int i;
	if(x<y)
	{//正着的
		x++;//从头顶绕过去:(x,1)->(y,1)
		if(!z)ans[++tot]=mkp(x,1);
		//走到头顶
		for(i=x+1;i<=y;i++)
		{
			assert(tot<=M);
			ans[++tot]=mkp(i,1);
		}
		if(!z)ans[++tot]=mkp(y,0);
		//返回对应点
	}
	if(x>y)
	{//反着的
		y++;//同上
		if(!z)ans[++tot]=mkp(x,0);
		for(i=x;i>y;i--)
		{
			assert(tot<=M);
			ans[++tot]=mkp(i,1);
		}
		if(!z)ans[++tot]=mkp(y,1);
	}
}
inline void l_r(int x,int y)
{//从 (x,1) 走向 (y,1)
	int i;
	for(i=x;i<=y;i++)
	{
		ans[++tot]=mkp(i,1);
	}
}
inline void r_l(int x,int y)
{//从 (x,1) 回到 (y,1)
	int i;
	for(i=x;i>=y;i--)
	{
		ans[++tot]=mkp(i,1);
	}
}
int n;
vector<int>vec;
inline void Slv(int x)
{//处理一个分割线处的状态 x
	assert(x);
	int i,k,t=0,fst=0,scd=0;
	for(i=n-1;~i;i--)
	{
		if((x>>i)&1)
		{
			t++;vec.epb(i);
		}//首先取出所有的点
	}
	vec.epb(vec.back()-1);
	//从(0,1)处出发
	l_r(0,vec.back());
	for(i=0,k=pre[0][--t].size();i<k;i++)
	{//选用点个数对应的方案
		fst=f(pre[0][t][i]);scd=s(pre[0][t][i]);
		//从方案中的pre[0][t][i-1]走到pre[0][t][i]
		if(i&&fst^f(pre[0][t][i-1])&&Abs(vec[fst]-vec[f(pre[0][t][i-1])])>1)
		{//这两个点中间跨了一些需要忽略的点
			assert(scd==s(pre[0][t][i-1]));
			Prt(vec[f(pre[0][t][i-1])],vec[fst],scd);
			//录入如何跨过这些点
		}
		assert(tot<=M);
		ans[++tot]=mkp(vec[fst]+1,scd);
		//录入当前点
	}
	//返回(0,1)处
	r_l(vec[fst]+1,1);
	vec.clear();
}
int top,a[Mx+5],stk[Mx+5];
inline int gt()
{
	char ch;
	for(ch=gc();ch^48&&ch^49;ch=gc());
	return ch==49;//读入'0'和'1'
}
signed main()
{
	#ifndef ONLINE_JUDGE
	freopen("_.in","r",stdin);
//	freopen("_.out","w",stdout);
	#endif
	int i,j,k,f;
	n=read();
	for(i=0;i<1<<n;i++)
	{
		for(f=!(a[i]=gt()),j=0;j<n;j++)
		{
			if(i&(1<<j))
			{
				k=i^(1<<j);
				if(a[i]&&!a[k])
				{//考察有没有限制的点多了反而合法的 不合法情况
					puts("Impossible");
					return output;
				}
				f&=a[k];
			}
		}
		if(f)
		{//i恰为分割线下的一点
			stk[++top]=i;
		}
	}
	puts("Possible");
	Pre(n);//预处理
	for(;top;Slv(stk[top--]));//处理分割线上的每个点
	writenum(tot,10);//输出
	assert(tot<=M);//我就是很松你能秒杀我?
	ans[++tot]=mkp(0,1);//别忘了把露出来的缺口填上
	for(i=1;i<=tot;i++)
	{
		writenum(f(ans[i]),32);writenum(s(ans[i]),10);
	}
	return output;
}