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
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 intok disjoint non-empty parts such thatp of the parts have even sum (each of them must have even sum) and remainingk -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 ≤ 105; 0 ≤ p ≤ k). The next line will containn space-separated distinct integers representing the content of arraya: a1, 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. Theith of them should contain the content of theith 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 exactlyp parts with even sum, each of the remainingk -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

题目大意:一组数组分成k部分,p组和为偶数的部分,k-p组和为奇数的部分,能够输出YES,否则输出NO

第3组的案例来讲,
1, 2 是偶的,

剩余的

3 ,5 1 3

1, 7

和是奇的

判断不难,主要是记录奇数的个数odd,如果odd>=k-p(需要的奇数组)并且剩余的奇数个数是偶数个(以配套凑成和为偶数的组合),同时偶数的个数even  +(剩余奇数能够凑成的最多的和为偶数的组合)>=p(即需要的偶数组);

所以一个条件语句就能控制

if(odd>=(k-p)&&(odd-(k-p))%2==0&&(even+(odd-(k-p))/2)>=p)
题目难处在于,后面的组合分配,在代码中详解。





AC代码如下:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<iomanip>
using namespace std;
#define ll long long
#define M 100005

struct H
{
    int x,y;//x为值,y为奇偶标记

}a[M];

int cmp(H q,H w)
{
    return q.y<w.y;
}

int main()
{
    int n,k,p,i,j,t=0;
    int even=0,odd=0,cont=0;
    cin>>n>>k>>p;
    for(i=0;i<n;i++)
    {
        cin>>a[i].x;

        if(a[i].x%2==0)
            {a[i].y=2;even++;}//标记偶数为2,统计偶数个数
        else {a[i].y=1;odd++;}//标记奇数为1,统计奇数个数
    }
    sort(a,a+n,cmp);//通过对标记值的排序,将数组奇数前置
        if(odd>=(k-p)&&(odd-(k-p))%2==0&&(even+(odd-(k-p))/2)>=p)
        {
            cout<<"YES"<<endl;
            if(p==0)//前面一直错就是卡在这,特殊情况的处理
            {
                for(i=1;i<=k;i++)
                {
                    if(i!=k)
                        {cout<<"1"<<" "<<a[t++].x<<endl;cont++;}//前面k-1都只输出一个数
                        else//i=k时把剩余的输出
                        {
                            cout<<n-cont<<" ";
                        for(j=1;j<=n-cont;j++)
                            cout<<a[t++].x<<" ";
                        cout<<endl;
                        }
                }
                return 0;
            }
            for(i=1;i<=k;i++)
            {
                if(i<=k-p)
                    {cout<<"1"<<" "<<a[t++].x<<endl;cont++;}//首先把奇数全输出
                else if(i>k-p&&i<k)
                {
                    if(a[t].y==1)//当还剩余奇数时,由于条件控制,它们肯定是偶数个,所以每两个奇数凑成偶数组
                    {
                        {cout<<"2"<<" "<<a[t++].x<<" "<<a[t++].x<<endl;cont+=2;}
                    }
                    else
                    {
                        {cout<<"1"<<" "<<a[t++].x<<endl;cont++;}
                    }
                }
                else//把剩余的输出
                    {
                        cout<<n-cont<<" ";
                        for(j=1;j<=n-cont;j++)
                            cout<<a[t++].x<<" ";
                        cout<<endl;
                    }
            }

        }
        else
            cout<<"NO"<<endl;
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值