题意是判断一个序列能否划分成一个单调增和一个单调减序列,空序列和只有一个元素的序列都算,能则输出两序列,不能则输出NO
思路是直接排序之后如果每个相同的元素只有两个就分别分到两序列,只有一个就随便分,有三个以上就输出NO
#include<bits/stdc++.h>
using namespace std;
int n, a[200005], in[200005], de[200005];
int main()
{
while (cin >> n) {
int ok = 0, k = 0, l = 0;
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a+n);
for (int i = 0; i < n; i++) {
if (i+2 < n && (a[i] == a[i+1] && a[i] == a[i+2])) {
cout << "NO" << endl;
ok = 1;
break;
}
if (i+1 < n && a[i] == a[i+1]) {
in[k++] = a[i];
de[l++] = a[i+1];
i++;
}
else {
de[l++] = a[i];
}
}
if (!ok) {
printf("YES\n");
printf("%d\n", k);
for (int i = 0; i < k; i++)
printf("%d ", in[i]);
printf("\n");
printf("%d\n", l);
for (int i = 0; i < l; i++)
printf("%d ", de[l-i-1]);
printf("\n");
}
}
return 0;
}