题目链接: 传送门

Devu and Partitioning of the Array

time limit per test:1 second     memory limit per test:256 megabytes

Description

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 n, k, p (1 ≤ k ≤ n ≤ 10^5; 0 ≤ p ≤ k). The next line will contain n space-separated distinct integers representing the content of array a: a1, a2, ..., an (1 ≤ ai ≤ 10^9).

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 Input

5 5 3
2 6 10 5 9

5 5 3
7 14 2 9 5

5 3 1
1 2 3 7 5

Sample Output

YES
1 9
1 5
1 10
1 6
1 2

NO

YES
3 5 1 3
1 7
1 2

解题思路:

题目大意:给n个数字,问能够将这n个数分成p堆,每堆和为偶数,k-p堆,每堆和为奇数
简单分堆,稍微注意一下细节处理。

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;

int main()
{
    int n,k,p;
    while (~scanf("%d%d%d",&n,&k,&p))
    {
        int tmp;
        vector<int>itv1,itv2;
        for (int i = 0; i < n; i++)
        {
            scanf("%d",&tmp);
            if (tmp & 1)
            {
                itv1.push_back(tmp);
            }
            else
            {
                itv2.push_back(tmp);
            }
        }
        int len1 = itv1.size();
        int len2 = itv2.size();
        if (len1 < (k - p) || ((len1 - (k - p))&1) || ((len1 - (k - p))/2 + len2 < p))
        {
            printf("NO\n");
        }
        else
        {
            int x = k - p;
            printf("YES\n");
            for (int i = 0;i < x - 1;i++)
            {
                printf("1 %d\n",itv1.back());
                itv1.pop_back();
            }
            for (int i = 0;i < p - 1;i++)
            {
                if (!itv2.empty())
                {
                    printf("1 %d\n",itv2.back());
                    itv2.pop_back();
                }
                else
                {
                    printf("2");
                    for (int j = 0;j < 2;j++)
                    {
                        printf(" %d",itv1.back());
                        itv1.pop_back();
                    }
                    printf("\n");
                }
            }
            if (x && p)
            {
                printf("1 %d\n",itv1.back());
                itv1.pop_back();
            }
            printf("%d",itv1.size()+itv2.size());
            while (!itv1.empty())
            {
                printf(" %d",itv1.back());
                itv1.pop_back();
            }
            while (!itv2.empty())
            {
                printf(" %d",itv2.back());
                itv2.pop_back();
            }
            printf("\n");
        }
    }
    return 0;
}

CF 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. CodeForce 439C Devu and Partitioning of the Array(模拟)

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

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

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

  4. 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 ...

  5. 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 ...

  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. 不得不玩玩NHibernate

    1.0=>前言 放着好好的EF不用,为什么要来玩NHibernate了?那是因为现在的工作内容就是维护一个比较老的项目,第一版是公司找外包做的,跟数据库打交道这块用的NHibernate,虽然都 ...

  2. jquery的offset与position的区别

    这里offset取得是屏幕影藏的y轴的距离➕元素距离屏幕的y轴的距离. 而postion取得的则是,上一个父元素(包含postion定位的)的距离

  3. jsonp的优缺点

    转载:http://www.w3cfuns.com/notes/18271/df9ecd8f0ca5e523ae75745a3996c47c.html JSONP的优缺点        1.优点    ...

  4. Altera OpenCL用于计算机领域的13个经典案例(转)

    英文出自:Streamcomputing 转自:http://www.csdn.net/article/2013-10-29/2817319-the-application-areas-opencl- ...

  5. kill 根据PID终止进程

    根据PID终止进程 kill [option] PID-list kill 通过向一个或多个进程发送信号来终止进程.除超级用户外,只有进程的所有者才可以对进程执行kill 参数 PID-list为ki ...

  6. Maven的内置变量

    Maven内置变量说明: ${basedir} 项目根目录(即pom.xml文件所在目录) ${project.build.directory} 构建目录,缺省为target目录 ${project. ...

  7. Android 自定义Popupwindow 注意事项,手机和平板的区别

    首先自定义ppw是要继承Popupwindow 的 而要成功的显示出自定义的ppw就必须实现下面的三句代码 // 必要的三要素下面,不然popWind显示不出来 this.setContentView ...

  8. java中的字符,字符串,数字之间的转换

    string 和int之间的转换 string转换成int  :Integer.valueOf("12") int转换成string : String.valueOf(12) ch ...

  9. CentOS 6.5升级Python和安装IPython(亲测可用)

    python的升级(2.6------>2.7.x) 如下地址:http://note.youdao.com/share/?id=2928aeda020123bfdf2a2c76bc75e4a7 ...

  10. 素数筛 poj 3518

    给你一个n 求包括n的一个非素数区间有多长 +1输出 #include<stdio.h> #include<string.h> #include<algorithm> ...