跳到正文

题解

P8866 喵了个喵 题解

E.Space约 9 分钟
原文
文章信息与版本

文章信息

发布
2022年12月8日
收录
2026年8月14日
更新
2026年11月16日
篇幅
约 9 分钟
获赞
67
题目
P8866

版本

  1. v1当前

这是我的简易题解:

k=2n−2k=2n-2 的时候直接留一个空栈,然后其它每个栈维护两个颜色。

这是我的 1515 分代码,如果想到做法的话,应该很好写:

C++
#include<bits/stdc++.h>
namespace io
{
	int F()
	{
		int n=0,F=0;
		char ch;
		while((ch=getchar())!='-'&&(ch<'0'||ch>'9'));
		ch=='-'?F=1:n=ch-'0';
		while((ch=getchar())>='0'&&ch<='9')n=n*10+ch-'0';
		return F?-n:n;
	}
}
int top[3333],bot[3333];
struct op{
	int v,s1,s2;
};
int main()
{
	int T=io::F();
	while(T--){
		std::vector<op> ops;
		int n=io::F(),m=io::F(),k=io::F();
		for(int i=1;i<=m;++i){
			int a=io::F();
			int pl=a+1>>1;
			if(bot[pl]==a){
				ops.push_back((op){1,n,0});
				ops.push_back((op){2,pl,n});
				bot[pl]=top[pl];
				top[pl]=0;
			}
			else{
				ops.push_back((op){1,pl,0});
				if(top[pl]==a)top[pl]=0;
				else if(bot[pl]==0)bot[pl]=a;
				else top[pl]=a;
			}
		}
		printf("%ld\n",ops.size());
		for(auto p: ops){
			if(p.v==1)printf("1 %d\n",p.s1);
				else printf("2 %d %d\n",p.s1,p.s2);
		}
	}
	return 0;
}

k=2n−1k=2n-1 的时候如果第 2n−12n-1 种颜色加入,那么检查下一种栈底元素或者自身出现的位置。如果自身先于任何一个栈底元素出现,那么将它放入空栈中。如果一个栈底元素先出现,那么检查这个元素上面的元素在它之前出现奇数次还是偶数次。如果是偶数次就把新元素插入在这个栈栈顶,最后用空栈消去栈底元素。如果是奇数次就把新元素插入空栈,然后从栈顶消去那个栈底元素。

不过我的主要目的是说说 n=2n=2 的部分分怎么做。发现大部分没有通过本题的选手都没有拿到这一档部分分。

第一种思路,也是我最先考虑到的思路是,n=2n=2 相当于往一根两端开口的管子里塞卡牌,两张相邻的卡牌消去。往管道的左边塞牌就相当于放入第一个栈。往右边塞牌就相当于放入第二个栈。消除时若两张相邻的牌在不同栈里,就使用一次操作 22。

然后考虑加入第三种颜色的牌的情况。这时看下一张牌的颜色。此时一定有一种方式使得加入这第三种颜色的牌之后,下一张牌没有被夹在中间。于是下一张牌进来时就可以消除了。这种方法很好写。

第二种思路是,如果两张相同的牌之间没有牌或者只有一张别的牌,那么可以考虑把这两张牌放在同一个栈里。如果它们之间有一张牌,那么就放在与这张牌放的栈不同的那个栈里。然后从序列中删去这两张牌。

到不能消之后剩下的牌会形如 123123123123... 的循环,这时候只要随便构造一下 123123 怎么消就行,然后不断重复就好了。这种方法很难写。

这是我的正解程序:

C++
#include<bits/stdc++.h>
namespace io
{
	int F()
	{
		int n=0,F=0;
		char ch;
		while((ch=getchar())!='-'&&(ch<'0'||ch>'9'));
		ch=='-'?F=1:n=ch-'0';
		while((ch=getchar())>='0'&&ch<='9')n=n*10+ch-'0';
		return F?-n:n;
	}
}
int last;
int a[2222222];
int pos[11111];
int top[3333],bot[3333];
int q[2222222],hq,tq;
struct op{
	int v,s1,s2;
	op(int x,int y,int z):v(x),s1(y),s2(z){}
};
void remove_top(std::vector<op>& ops, int& p){
	ops.emplace_back(1,p,0);
	top[p]=0;
	q[tq++]=p;
	p=0;
}
void remove_bot(std::vector<op>& ops, int& p){
	ops.emplace_back(1,last,0);
	ops.emplace_back(2,-p,last);
	pos[top[-p]]*=-1;
	bot[-p]=top[-p];
	top[-p]=0;
	q[tq++]=-p;
	p=0;
}
void push(std::vector<op>& ops, int c){
	int pl=q[hq++];
	ops.emplace_back(1,pl,0);
	if(bot[pl]){
		top[pl]=c;
		pos[c]=pl;
	}
	else{
		bot[pl]=c;
		pos[c]=-pl;
	}
}
int main()
{
	int T=io::F();
	while(T--){
		int n=io::F(),m=io::F(),k=io::F();

		for(int i=1;i<=m;++i){
			a[i]=io::F();
		}
		hq=tq=1;
		for(int i=1;i<n;++i){
			q[tq++]=i;
			q[tq++]=i;
		}
		last=n;
		std::vector<op> ops;
		for(int i=1;i<=m;++i){
			int &p=pos[a[i]];
			if(p>0){
				remove_top(ops,p);
			}
			else if(p<0){
				remove_bot(ops,p);
			}
			else if(hq!=tq){
				push(ops,a[i]);
			}
			else{
				int j=i+1;
				while(pos[a[j]]>0)++j;
				if(a[i]==a[j]){
					ops.emplace_back(1,last,0);
					for(++i;i<j;++i){
						if(pos[a[i]]){
							remove_top(ops,pos[a[i]]);
						}
						else{
							push(ops,a[i]);
						}
					}
					ops.emplace_back(1,last,0);
				}
				else{
					int cnt=0;
					int cr=top[-pos[a[j]]],crp=pos[cr];
					int now=a[i];
					for(int l=i+1;l<j;++l){
						cnt^=a[l]==cr;
					}
					if(cnt){
						ops.emplace_back(1,last,0);
						for(++i;i<j;++i){
							if(pos[a[i]]){
								remove_top(ops,pos[a[i]]);
								if(a[i]==cr)--tq;
							}
							else if(a[i]==cr){
								q[--hq]=crp;
								push(ops,cr);
							}
							else{
								push(ops,a[i]);
							}
						}
						ops.emplace_back(1,crp,0);
						bot[last]=now;
						pos[a[i]]=0;
						pos[now]=-last;
						q[tq++]=last;
						last=crp;
						bot[last]=0;
					}
					else{
						ops.emplace_back(1,crp,0);
						for(++i;i<j;++i){
							if(a[i]==cr){
								ops.emplace_back(1,last,0);
							}
							else if(pos[a[i]]){
								remove_top(ops,pos[a[i]]);
							}
							else{
								push(ops,a[i]);
							}
						}
						ops.emplace_back(1,last,0);
						ops.emplace_back(2,crp,last);
						pos[a[i]]=0;
						pos[cr]*=-1;
						pos[now]=crp;
						bot[crp]=cr;
						top[crp]=now;
					}
				}
			}
		}
		printf("%ld\n",ops.size());
		for(auto p: ops){
			if(p.v==1)printf("1 %d\n",p.s1);
				else printf("2 %d %d\n",p.s1,p.s2);
		}
	}
	return 0;
}
原文
存档评论12 条抓取于