给一个长度为n的序列,让你分成k份,每份数目若干,要求和为偶数的个数为k。
读入时候分奇数偶数保存,判断是否可行,而后一个个的分配输出。
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int maxn = 1e5 + 5;
int main(int argc, char const *argv[])
{
int n, p, k;
while(scanf("%d%d%d", &n, &k, &p) != EOF) {
int a[maxn], b[maxn], c, odd = 0, even = 0;
for(int i = 0; i < n; ++i) {
scanf("%d", &c);
if(c & 1) a[odd++] = c;
else b[even++] = c;
}
if(odd < k - p || (odd - k + p) % 2 == 1 || even + (odd - k + p) / 2 < p) printf("NO\n");
else {
printf("YES\n");
int temp = k - p;
for(int i = 0; i < temp - 1; ++i)
printf("1 %d\n", a[--odd]);
for(int i = 0; i < p - 1; ++i)
if(even) printf("1 %d\n", b[--even]);
else {
printf("2 %d %d\n", a[odd - 1], a[odd - 2]);
odd -= 2;
}
if(temp && p) printf("1 %d\n", a[--odd]);
printf("%d", odd + even);
while(odd) printf(" %d", a[--odd]);
while(even) printf(" %d\n", b[--even]);
printf("\n");
}
}
return 0;
}