思路:
(1)题目要求将集合A划分为B,C两组,使得C中任意数都不是B中的除数
(2)直观感受,只要让C中数比B中大,则满足条件,不妨只取最大的放入C中;
(3)若不能如此划分,即A中所有数都一样大,则一定不能满足条件;
(4)这意味着对于这种划分方法,能划分就一定能划分,不能划分就一定不能划分,于是只要输入后,找到最大值及其数量,若等于n则输出-1,否则输出cnt个极大值,再输出所有非极大值即可。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>using namespace std;typedef long long LL;const int N = 1e3 + 10;
LL a[N];
LL n;int main()
{int t;cin >> t;while(t --){cin >> n;LL Max = 0, cnt = 0;for (int i = 1; i <= n; i++){cin >> a[i];Max = max(a[i], Max);}for (int i = 1; i <= n; i++){if (a[i] == Max)cnt++;}if (cnt==n){cout << "-1" << endl;continue;}cout << n - cnt << ' ' << cnt << '\n';for (int i = 1; i <= n; i++){if (a[i]!=Max)cout << a[i] << ' ';}cout << endl;for (int i = 1; i <= cnt; i++){cout << Max << ' ';}cout << endl;}return 0;
}