题目链接: 传送门

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. swift——uiwebview的使用

    首先,创建一个label: agreeDeal = UILabel() let tap = UITapGestureRecognizer.init(target: self, action: #sel ...

  2. lecture9-提高模型泛化能力的方法

    HInton第9课,这节课没有放论文进去.....如有不对之处还望指正.话说hinton的课果然信息量够大.推荐认真看PRML<Pattern Recognition and Machine L ...

  3. 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)

    传统解法 题目来自 leetcode 335. Self Crossing. 题意非常简单,有一个点,一开始位于 (0, 0) 位置,然后有规律地往上,左,下,右方向移动一定的距离,判断是否会相交(s ...

  4. .Net简单图片系统-本地存储和分布式存储

    本地存储 所谓本地存储就是将上传图片保存到图片服务器的本地磁盘上. if (ConfigHelper.GetConfigString("SaveMode") == "Lo ...

  5. 转载:SQL 递归树 子父节点相互查询

    if object_id('[tb]') is not null drop table [tb] go create table [tb]([modeid] int,modename varchar( ...

  6. jquery的offset与position的区别

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

  7. mysql创建触发器

    触发器语句只有一句话 可以省略begin和end CREATE trigger `do_praise` after insert on praise for each row update post ...

  8. dmesg 显示内核消息

    显示内核消息 dmesg [options] dmesg 可以用来显示存储在内核环缓冲区中的消息 系统启动时,内核会用硬件和模块初始化的相关消息填充其环缓冲区.内核环缓冲区中的消息常常用于诊断系统问题 ...

  9. Beta项目冲刺--第三天

    又找回熟悉的感觉.... 队伍:F4 成员:031302301 毕容甲 031302302 蔡逸轩 031302430 肖阳 031302418 黄彦宁 会议内容: 1.站立式会议照片: 2.项目燃尽 ...

  10. 延时程序执行Qt

    有时候为了让程序暂停一下,不让它一直跑下去,可以使它进入循环结构中! 例如: #include <QCoreApplication> #include <qdebug.h> # ...