Devu and Partitioning of the Array
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Devu being a small kid, likes to play a lot, but he only likes to play with arrays. While playing he came up with an interesting question which he could not solve, can you please solve it for him?

Given an array consisting of distinct integers. Is it possible to partition the whole array into k disjoint non-empty parts such that p of
the parts have even sum (each of them must have even sum) and remaining k - p have
odd sum? (note that parts need not to be continuous).

If it is possible to partition the array, also give any possible way of valid partitioning.

Input

The first line will contain three space separated integers nkp (1 ≤ k ≤ n ≤ 105; 0 ≤ p ≤ k).
The next line will contain n space-separated distinct integers representing the content of array aa1, a2, ..., an (1 ≤ ai ≤ 109).

Output

In the first line print "YES" (without the quotes) if it is possible to partition the array in the required way. Otherwise print "NO"
(without the quotes).

If the required partition exists, print k lines after the first line. The ith of
them should contain the content of the ith part.
Print the content of the part in the line in the following way: firstly print the number of elements of the part, then print all the elements of the part in arbitrary order. There must be exactly p parts
with even sum, each of the remaining k - p parts
must have odd sum.

As there can be multiple partitions, you are allowed to print any valid partition.

Sample test(s)
input
5 5 3
2 6 10 5 9
output
YES
1 9
1 5
1 10
1 6
1 2
input
5 5 3
7 14 2 9 5
output
NO
input
5 3 1
1 2 3 7 5
output
YES
3 5 1 3
1 7
1 2

题意:给出n个数。要分成k份。每份有若干个数。可是仅仅须要关注该份的和为奇数还是偶数,要求偶数堆的个数为p。输出方案。

分析:先输出k-p-1组奇数,然后输出p-1组偶数;假设 p!=0&&(k-p)!=0,再输出一个奇数,这时奇数的已经构造完毕。最后把剩下的所有输出为一组就可以。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int MAXN = 1e5 + 100;
int a[MAXN], b[MAXN];
int main()
{
int n, p, k, i, odd = 0, even = 0, c;
scanf("%d%d%d",&n,&k,&p);
for(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 tmp = k - p;
for(i = 0; i < tmp - 1; i++)
printf("1 %d\n",a[--odd]);
for(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(tmp && 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;
}

CodeForce 439C Devu and Partitioning of the Array(模拟)的更多相关文章

  1. Codeforces 439C Devu and Partitioning of the Array(模拟)

    题目链接:Codeforces 439C Devu and Partitioning of the Array 题目大意:给出n个数,要分成k份,每份有若干个数,可是仅仅须要关注该份的和为奇数还是偶数 ...

  2. CF 439C Devu and Partitioning of the Array

    题目链接: 传送门 Devu and Partitioning of the Array time limit per test:1 second     memory limit per test: ...

  3. codeforces 251 div2 C. Devu and Partitioning of the Array 模拟

    C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...

  4. codeforces 439C Devu and Partitioning of the Array(烦死人的多情况的模拟)

    题目 //这是一道有n多情况的烦死人的让我错了n遍的模拟题 #include<iostream> #include<algorithm> #include<stdio.h ...

  5. CF 439C(251C题)Devu and Partitioning of the Array

    Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabytes ...

  6. Codeforces Round #251 (Div. 2) C. Devu and Partitioning of the Array

    注意p的边界情况,p为0,或者 p为k 奇数+偶数 = 奇数 奇数+奇数 = 偶数 #include <iostream> #include <vector> #include ...

  7. codeforces 439D Devu and Partitioning of the Array(有深度的模拟)

    题目 //参考了网上的代码 注意答案可能超过32位 //要达成目标,就是要所有数列a的都比数列b的要小或者等于 //然后,要使最小的要和最大的一样大,就要移动(大-小)步, //要使较小的要和较大的一 ...

  8. codeforces C. Devu and Partitioning of the Array

    题意:给你n个数,然后分成k部分,每一个部分的和为偶数的有p个,奇数的有k-p个,如果可以划分,输出其中的一种,不可以输出NO; 思路:先输出k-p-1个奇数,再输出p-1个偶数,剩余的在进行构造.  ...

  9. 【Henu ACM Round#20 D】 Devu and Partitioning of the Array

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 一开始所有的数字单独成一个集合. 然后用v[0]和v[1]记录集合的和为偶数和奇数的集合它们的根节点(并查集 然后先让v[0]的大小 ...

随机推荐

  1. sqlserver 分页查询总结

    sqlserver2008不支持关键字limit ,所以它的分页sql查询语句将不能用mysql的方式进行,幸好sqlserver2008提供了top,rownumber等关键字,这样就能通过这几个关 ...

  2. [转]apache的源码安装详细过程全纪录

    原文链接:http://www.jb51.net/article/59474.htm 文中  开机启动需要修改 而且特别麻烦 还的配置php   否则不认识php文件  郁闷!只能做参考了!

  3. pycharm+QT4的helloworld

    # -*- coding: utf-8 -*- from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 exc ...

  4. BFS 、DFS 解决迷宫入门问题

    问题 B: 逃离迷宫二 时间限制: 1 Sec  内存限制: 128 MB提交: 12  解决: 5[提交][状态][讨论版] 题目描述 王子深爱着公主.但是一天,公主被妖怪抓走了,并且被关到了迷宫. ...

  5. php三元运算

    $a = 2; $a == 1 ? $test="企业" : ($a==2 ? $test="地区" : $test="其他地方"); ec ...

  6. JMS和消息驱动Bean(MDB)

    一.说明 本示例使用的ActiveMQ作为消息中间件,服务器为Glassfish,使用JMS发送消息,在MDB接收到消息之后做打印输出. 二.ActiveMQ安装配置 1.安装console war包 ...

  7. 基于visual Studio2013解决算法导论之029二叉搜索树

     题目 二叉搜索树 解决代码及点评 #include <stdio.h> #include <malloc.h> #include <stdlib.h> ty ...

  8. (step7.2.3)hdu 2554(N对数的排列问题——简单数论)

    题目大意:输入一个整数n,表示有n对整数.判断能否出现一种情况就是2个1之间有1个数,2个2之间有2个数..... 解题思路: 准备知识: ①n对数,共2*n个数.所以要有2*n个位置来放置这2*n个 ...

  9. php - 小型微博系统

    效果: 数据库: 项目结构: add.php : 添加微博. conn.php : 数据库配置文件. delete.php : 删除博客代码. disinfo.php : 显示微博详细信息. inde ...

  10. SQL之概念

    SQL即结构化查询语言,是一个功能强大的数据库语言,可以分为: 1.DML即数据操作语言,用于检索或者修改数据: 2.DDL即数据定义语言,用于定义数据的结构,如创建.修改.删除等: 3.DCL即数据 ...