跳到正文

题解

P6676 [COCI2019-2020#2] Slagalica题解

三重门123456约 11 分钟
原文
文章信息与版本

文章信息

发布
2021年11月21日
收录
2026年5月2日
更新
2026年11月3日
篇幅
约 11 分钟
题目
P6676

版本

  1. v2当前
  2. v1

本题解使用向量(动态数组,vector)实现,代码为当前最快。(开O2时)

有链接为证: https://www.luogu.com.cn/record/61822682

让我们简化题目:

有n会拼图,摆成一条线,左右有限制条件:凹凸要相对应。 注意:拼图不能旋转! 若每块拼图有一个互不相同的正整数,输出合法且字典序最小的排列。

接下来分析题意:

由于拼图只有左右边有用,且有自己的编号和类型,因此我们可以这样存图:

C++
vector <int> tu[10];//拼图[i]及其编号
struct figure{bool l,r;}F[10];//使用结构体存储数据:0:凹  1:凸

并将其初始化

C++
int ALL;
int CHECK[]={0,2,0,0,-2,1,-1,1,-1};//凸++,凹--;
int l=0,r=0;//最左边,最右边的编号
void Init(){
	F[1]=(figure){1,1};F[2]=(figure){1,0};F[3]=(figure){0,1};F[4]=(figure){0,0};
	F[5]=(figure){0,1};F[6]=(figure){0,0};F[7]=(figure){1,0};F[8]=(figure){0,0};
	if(tu[5].size()==1)l=5;else l=6;
	if(tu[7].size()==1)r=7;else r=8;
;
}

其中,F为各个拼图的左右特征,到时候就不必费力挨个比较了。

知道了特征,我们就可以写出以下主函数:

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=1e5+7;
int ans[maxn],tot;
int main( ) {
	int n=R();
	for(register int i=1,x,a;i<=n;i++){
		x=R(),a=R();
		ALL+=CHECK[x];
		tu[x].push_back(a);
	}
	Init();//初始化
	for(register int i=1;i<=4;i++){
		sort(tu[i].begin(),tu[i].end());
	}//只用对其进行一次排序即可
	if(sj_no_ans()){判断是否合法
		puts("-1");
		return 0;
	}
	work(n);//工作函数
	for(register int i=1;i<=tot;i++){
		printf("%d ",ans[i]);
	}
	return 0;
}

好了,框架打好了,接下来我们来写两大部分:特判,工作函数。

特判

先给代码:

C++
bool sj_no_ans(){
	if((l==5&&r==7)||(l==6&&r==8)){
		if(ALL)return true;//若左右拼图凹凸总数不相等,必然无解
	}else{
		if(ALL)return true;//同上
		if(l==5&&r==8)
			if(tu[2].size())
				if(!tu[1].size()&&!tu[2].size())
					return true;
		else if(l==6&&r==7)
			if(tu[3].size())
				if(!tu[1].size()&&!tu[2].size())
					return true;
	}
	return false;//有解
}

其中ALL为左右边凹凸之和,若有几个突出,加几,几个凹陷减去几。 想想,若凹凸数量都不相等(和为0),那么能匹配上吗?必然不行啊!

分类讨论:若都是突出或凹陷,我们就只用求出其和即可。 而若一边凸一边凹,则可能有以下情况: 5 2222 7 虽然凹凸相等,但是还是不行。

工作函数

C++
void work(int n){
	figure last=F[l];//是1还是4
	figure wei=F[r];//末尾
  //数字化首尾的形状
	for(int i=0;i<=4;i++){
		Begin[i]=0;
		End[i]=tu[i].size();
	}//if(begin[i]<end[i]),则不为empty()
	//0:凹     1:凸
	ans[++tot]=tu[l][0];//最左边的一定是第一个方块
	while(check()){//接下来会给出
		if(last.r==1){如果为右边凸,左边就要接凹(wei.l==0)的
			while(Begin[3]<End[3]&&
			((End[4]-Begin[4]==1&&wei.l==1)||Begin[4]>=End[4]||tu[3][Begin[3]]<tu[4][Begin[4]])//判断条件
					 )ans[++tot]=tu[3][Begin[3]++];//一口气把能放上的3都放上
			if(Begin[4]<End[4]){
				ans[++tot]=tu[4][Begin[4]++];
				last=F[4];
			}//3不满足条件了,放一个4
		}else{如果为右边凹,左边就要接凸(wei.l==1)的
			while(Begin[2]<End[2]&&
			((End[1]-Begin[1]==1&&wei.l==0)||(Begin[1]>=End[1]||tu[2][Begin[2]]<tu[1][Begin[1]]))
					 )ans[++tot]=tu[2][Begin[2]++];	//一口气把能放上的2都放上
			if(Begin[1]<End[1]){
				ans[++tot]=tu[1][Begin[1]++];
				last=F[1];
			}//2不满足条件了,放一个1
		}
	}
	ans[++tot]=tu[r][0];//其他方块都放完了,把最后一个放上。
}

check()

C++
inline bool check(){//检查是否用完了
	static int i;//这样就不用反复的开辟删除i了
	for(i=1;i<=4;i++){
		if(Begin[i]<End[i])return true;
	}
	return false;
}

注意一点小细节

vector不像数组,若越界,就会RE!(这就是窝反复RE却找不到问题的原因),但是我们却可以用一些方法规避掉:

C++
//first one
vector <int> V;
V.reserve(你要开辟的个数);
//本函数的作用是给你的vector预留一段空间,防止越界。
//因为是预留,所以要用push_back()操作,size()返回的是你存入的真实数据规模。
//second one
vector <int> V;
V.resize(你要开辟的个数);
////本函数的作用是给你的vector直接开辟一段空间,防止越界。
//因为是直接开辟,所以直接当数组操作即可,size()返回的是你开辟的数据规模。
//两种函数各有千秋,建议具体题目具体使用。

OK,接下来是欢快的整理时间!

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=1e5+7;
vector <int> tu[10];//tu[i]有多少个
int ALL;
int CHECK[]={0,2,0,0,-2,1,-1,1,-1};//凸++,凹--;
int l=0,r=0;//左右的编号
struct figure{bool l,r;}F[10];//0:凹     1:凸
void Init(){
	F[1]=(figure){1,1};F[2]=(figure){1,0};F[3]=(figure){0,1};F[4]=(figure){0,0};
	F[5]=(figure){0,1};F[6]=(figure){0,0};F[7]=(figure){1,0};F[8]=(figure){0,0};
	if(tu[5].size()==1)l=5;else l=6;
	if(tu[7].size()==1)r=7;else r=8;
	return;
}
bool sj_no_ans(){
	if((l==5&&r==7)||(l==6&&r==8)){
		if(ALL)return true;//若左右拼图凹凸总数不相等,必然无解
	}else{
		if(ALL)return true;//同上
		if(l==5&&r==8)
			if(tu[2].size())
				if(!tu[1].size()&&!tu[2].size())
					return true;
		else if(l==6&&r==7)
			if(tu[3].size())
				if(!tu[1].size()&&!tu[2].size())
					return true;
	}
	return false;//有解
}
int ans[maxn],tot;
int Begin[5],End[5];//开始,结束
inline bool check(){//检查是否用完了
	static int i;//这样就不用反复的开辟删除i了
	for(i=1;i<=4;i++){
		if(Begin[i]<End[i])return true;
	}
	return false;
}
void work(int n){
	figure last=F[l];//是1还是4
	figure wei=F[r];//末尾
  //数字化首尾的形状
	for(int i=0;i<=4;i++){
		Begin[i]=0;
		End[i]=tu[i].size();
	}//if(begin[i]<end[i]),则不为empty()
	//0:凹     1:凸
	ans[++tot]=tu[l][0];//最左边的一定是第一个方块
	while(check()){//接下来会给出
		if(last.r==1){//如果为右边凸,左边就要接凹(wei.l==0)的
			while(Begin[3]<End[3]&&
			((End[4]-Begin[4]==1&&wei.l==1)||Begin[4]>=End[4]||tu[3][Begin[3]]<tu[4][Begin[4]])//判断条件
					 )ans[++tot]=tu[3][Begin[3]++];//一口气把能放上的3都放上
			if(Begin[4]<End[4]){
				ans[++tot]=tu[4][Begin[4]++];
				last=F[4];
			}//3不满足条件了,放一个4
		}else{//如果为右边凹,左边就要接凸(wei.l==1)的
			while(Begin[2]<End[2]&&
			((End[1]-Begin[1]==1&&wei.l==0)||(Begin[1]>=End[1]||tu[2][Begin[2]]<tu[1][Begin[1]]))
					 )ans[++tot]=tu[2][Begin[2]++];	//一口气把能放上的2都放上
			if(Begin[1]<End[1]){
				ans[++tot]=tu[1][Begin[1]++];
				last=F[1];
			}//2不满足条件了,放一个1
		}
	}
	ans[++tot]=tu[r][0];//其他方块都放完了,把最后一个放上。
}
inline int R();
int main( ) {
	int n=R();
	for(register int i=1,x,a;i<=n;i++){
		x=R(),a=R();
		ALL+=CHECK[x];
		tu[x].push_back(a);
	}
	Init();//初始化
	for(register int i=1;i<=4;i++){
		sort(tu[i].begin(),tu[i].end());
	}//只用对其进行一次排序即可
	if(sj_no_ans()){//判断是否合法
		puts("-1");
		return 0;
	}
	work(n);//工作函数
	for(register int i=1;i<=tot;i++){
		printf("%d ",ans[i]);
	}
	return 0;
}

inline int R() {
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
	while('0'<=ch&&ch<='9') {x=x*10+ch-48;ch=getchar();}
	return x*f;
}
祝大家使用愉快!
原文