codeforces 439C Devu and Partitioning of the Array(簡單題)

題目鏈接

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

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
題意:n個數字,把他們分到K個集合,使得有P個集合中數的和爲偶數,另外k-p個集合的數的和爲奇數。如果有解,並任意一種輸出方案。

題解:由於只要求和的奇偶性,所以我們只用考慮數字的奇偶性,先提取出所有的奇數和偶數,然後先將要求和爲奇數的集合放一個奇數,放完後把和爲偶數的集合放一個偶數或兩個奇數。放完以後,如果剩餘的奇數有奇數個,則無解。否則剩餘的偶數隨便放,剩餘的奇數兩個爲一組隨便放即可。

代碼如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<string.h>
#include<string>
#include<stdlib.h>
typedef __int64 LL;
typedef unsigned __int64 LLU;
const int nn=110000;
const int inf=0x3fffffff;
const LL inf64=(LL)inf*inf;
using namespace std;
int n,k,p;
queue<int>odd,even;
vector<int>ve[nn];
int main()
{
    int i,x;
    while(scanf("%d%d%d",&n,&k,&p)!=EOF)
    {
        for(i=1;i<=k;i++)
        {
            ve[i].clear();
        }
        while(odd.size())
            odd.pop();
        while(even.size())
            even.pop();
        for(i=1;i<=n;i++)
        {
            scanf("%d",&x);
            if(x%2)
            {
                odd.push(x);
            }
            else
                even.push(x);
        }
        if(odd.size()<k-p)
        {
            puts("NO");
            continue;
        }
        for(i=1;i<=k-p;i++)
        {
            ve[i].push_back(odd.front());
            odd.pop();
        }
        if(odd.size()%2==1)
        {
            puts("NO");
            continue;
        }
        for(i=k-p+1;i<=k;i++)
        {
            if(odd.size())
            {
                ve[i].push_back(odd.front());
                odd.pop();
                ve[i].push_back(odd.front());
                odd.pop();
            }
            else if(even.size())
            {
                ve[i].push_back(even.front());
                even.pop();
            }
            else
                break;
        }
        if(i<=k)
        {
            puts("NO");
            continue;
        }
        else
        {
            while(even.size())
            {
                ve[1].push_back(even.front());
                even.pop();
            }
            while(odd.size())
            {
                ve[1].push_back(odd.front());
                odd.pop();
            }
            puts("YES");
            for(i=1;i<=k;i++)
            {
                int lv=ve[i].size();
                printf("%d",lv);
                for(int j=0;j<lv;j++)
                {
                    printf(" %d",ve[i][j]);
                }
                puts("");
            }
        }
    }
    return 0;
}


發佈了213 篇原創文章 · 獲贊 4 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章