CF1859A 题解

发布时间 2023-08-14 16:21:11作者: SunnyYuan

题意

给定一个数组 \(a\),然后将 \(a\) 分到数组 \(b\)\(c\) 中,使得 \(c\)\(b\) 都不为空且 \(c\) 中的任何一个数字都不是 \(b\) 中任何一个数字的因数。

思路

我们知道,在正整数中,一个数字 \(x\) 只要大于数字 \(y\),那么 \(x\) 一定不是 \(y\) 的因数,

比如 \(5\) 不是 \(3\) 的因数,\(7\) 不是 \(1\) 的因数。

所以我们只要把数列中的最大值单独拿出来作为 \(c\) 数组就可以了,其他数字都放到 \(b\) 数组,

这样 \(c\) 中的数字一定大于 \(b\) 中的任何一个数字,那么 \(c\) 中的任何一个数字都不是 \(b\) 中任何一个数字的因数,符合题意。

代码

代码非常简单:

#include <bits/stdc++.h>

using namespace std;

const int N = 110;

int n;
int a[N];

bool check() {
	for (int i = 1; i <= n; i++) {
		if (a[i] != a[1]) {
			return false;
		}
	}
	return true;
}

void solve() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	int maxx = 0, p = 0;
	for (int i = 1; i <= n; i++) {
		if (maxx < a[i]) {
			maxx = a[i];
		}
	}
	if (check()) {
		cout << -1 << '\n';
		return;
	}
	else {
		vector<int> a1, a2; 
		for (int i = 1; i <= n; i++) {
			if (a[i] == maxx) a2.push_back(maxx);
			else a1.push_back(a[i]);
		}
		cout << a1.size() << ' ' << a2.size() << '\n';
		for (int x : a1) cout << x << ' ';
		cout << '\n';
		for (int x : a2) cout << x << ' ';
		cout << '\n';
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int T;
	cin >> T;
	while (T--) solve();
	return 0; 
}