跳到正文

题解

题解:P12566 [UOI 2023] An Array and Several More Arrays

Reply_约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年11月5日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 3 分钟
题目
P12566

版本

  1. v2当前
  2. v1

这不暴力吗。

显然最后要充满 [1,n][1,n]。将每个数组内部排序,枚举每个数组最小的数出现的顺序,枚举到某个数组的时候,直接钦定这个数组的最小值变成当前整个区间的最小的没有被填充的值,用 set 之类的东西维护即可。

C++
#include<bits/stdc++.h>
#define ll long long
#define R register
#define F(i,a,b) for(int i = (a);i<=(b);i++)
using namespace std;
inline int read(){R int x=0,t=1;R char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') t=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*t;}
const int N=2e5+10;
int n,k;
int a[6][N],b[N],d[N],c[N];
bool solve()
{
	set<int>s;
	for(int i = 1;i<=n;i++){
		s.insert(i);
	}
	for(int i = 1;i<=k;i++){
		int x=*s.begin();
		int id=b[i];
		d[id]=x-a[id][1];
		s.erase(x);
		for(int j = 2;j<=a[id][0];j++){
			if(s.count(a[id][j]+d[id])){
				s.erase(a[id][j]+d[id]);
			}
			else{
				return 0;
			}
		}
	}
//	F(i,1,k) cout << b[i] << '\n';
	return 1;
}

signed main()
{
//	freopen("P12566_33.in","r",stdin);
	n=read(),k=read();
	for(int i = 1;i<=k;i++){
		vector<int>ls;
		a[i][0]=read();
		F(j,1,a[i][0]){
			a[i][j]=read();
			ls.push_back(a[i][j]);
		}
		sort(ls.begin(),ls.end());
		F(j,1,a[i][0]){
			a[i][j]=ls[j-1];
		}
	}
	F(i,1,k) b[i]=i;
	do{
		if(solve()){
			puts("Yes");
			for(int i = 1;i<=k;i++){
				cout << d[i] << " ";
			}
			return 0;
		}

	}while(next_permutation(b+1,b+1+k));
	puts("No");
	return 0;
}
/*

*/
原文