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
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 n, k, p (1 ≤ k ≤ n ≤ 105; 0 ≤ p ≤ k). The next line will contain n space-separated distinct integers representing the content of array a: 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. 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.
Examples
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

原理很简单,但是做的时候好难啊,emmm…
好吧,我承认我是卡数据过的

解析:
输出yes的条件有三个,详见代码
然后分情况吧,当p不为零时,我们直接给k-p个序列每个一个奇数,然后剩下p个序列让奇数两个成对加进去,剩下的补偶数
当p为零时,给k-p-1个序列每个一个奇数,剩下的数字全部给剩下的一个序列

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=1e5+100;
int arr[N];
int vis[N];
int nu[N];
int nu1[N];
vector<int>G[N];
int main()
{
    int n,k,p;
    cin>>n>>k>>p;
    int num=0,num1=0;
    rep(i,0,n)
    {
        cin>>arr[i];
        if(arr[i]%2!=0)
            nu[num++]=arr[i];
        else
            nu1[num1++]=arr[i];
    }
    int a=num-(k-p);
    if(a>=0&&a%2==0&&(a/2+num1)>=p)
    {
        cout<<"YES"<<endl;
        if(p==0)
        {
            int cnt=0;
            for(int i=0;i<num;i++)
            {
                if(cnt==k-1)
                    while(i<num)
                    {
                        G[cnt].pb(nu[i++]);
                    }
                else
                    G[cnt++].pb(nu[i]);
            }
            for(int i=0;i<num1;i++)
                G[k-1].pb(nu1[i]);
            for(int i=0;i<k;i++)
            {
                cout<<G[i].size()<<' ';
                for(int j=0;j<G[i].size();j++)
                    cout<<G[i][j ]<<' ';
                cout<<endl;
            }
            return 0;
        }
        for(int i=0;i<k-p;i++)
            cout<<1<<' '<<nu[i]<<endl;
        int cnt=0,i=k-p;
        for(i;i<num;i+=2)
        {
            G[cnt].pb(nu[i]);
            G[cnt++].pb(nu[i+1]);
            if(cnt==p)
                break;
        }
        i+=2;
        for(i;i<num;i++)
            G[0].pb(nu[i]);
        int j=p-1;
        for(int l=0;l<num1;l++)
        {
            if(j!=0&&G[j].size()==0)
                G[j--].pb(nu1[l]);
            else
            {
                while(l<num1)
                {
                    G[j].pb(nu1[l++]);
                }
                break;
            }
        }
        for(int l=0;l<p;l++)
        {
            cout<<G[l].size()<<' ';
            for(int q=0;q<G[l].size();q++)
                cout<<G[l][q]<< ' ';
            cout<<endl;
        }
    }
    else
        cout<<"NO"<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值