题目链接: 传送门

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. leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  2. Redis百亿级Key存储方案

    1 需求背景 该应用场景为DMP缓存存储需求,DMP需要管理非常多的第三方id数据,其中包括各媒体cookie与自身cookie(以下统称supperid)的mapping关系,还包括了supperi ...

  3. GWT-Dev-Plugin(即google web toolkit developer plugin)for Chrome的安装方法

    如果你想要在Chrome中进行GWT调试,需要安装“gwt developer plugin for chrome”,但是普通安装模式下,会提示: This application is not su ...

  4. 硬件升级win8.1重新安装系统

    上次重装系统一年后升级了硬件配置,双11后,再次折腾系统. 配置硬件的选择 部分配件已经升级过了,之前的一直是AMD平台,发热大功耗高,08年我配的AMD 8450+映泰GX790 128M待机在10 ...

  5. ContentObserver监听数据库·变化

    //短信Uri Uri smsUri = Uri.parse("content://sms"); //使用ContentReslover注册·监听器 getContentResol ...

  6. colpick-jQuery颜色选择器使用说明

      一.demo及下载网址:http://www.htmleaf.com/jQuery/Color-Picker/20141108417.html   二.使用效果   三.使用方法 1.引入js和c ...

  7. hive中行转换成列

    python代码 #!/bin/bashimport sys; if __name__=='__main__': for line in sys.stdin: m=line.strip().split ...

  8. react.js table组件【可以直接使用】

    最近在做一个CMS,使用的技术是刚刚学习的react.js,准备制作一个查询的页面以及一个新增的页面. 这是table的公共组件: 我们在使用的过程中,只会用到: 制作出来的查询页面: 新增页面: 上 ...

  9. hdu4990 矩阵

    C - Reading comprehension Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  10. jsrender for 标签

    for object使用 1.为进入object中直接使用其中的属性 <div id="result"></div> <script id=" ...