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
  1. 5 5 3
  2. 2 6 10 5 9
Output
  1. YES
  2. 1 9
  3. 1 5
  4. 1 10
  5. 1 6
  6. 1 2
Input
  1. 5 5 3
  2. 7 14 2 9 5
Output
  1. NO
Input
  1. 5 3 1
  2. 1 2 3 7 5
Output
  1. YES
  2. 3 5 1 3
  3. 1 7
  4. 1 2

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

第3组的案例来讲,

1, 2 是偶的,

剩余的

3 。5 1 3

1, 7

和是奇的

推断不难,主要是记录奇数的个数odd,假设odd>=k-p(须要的奇数组)而且剩余的奇数个数是偶数个(以配套凑成和为偶数的组合),同一时候偶数的个数even  +(剩余奇数可以凑成的最多的和为偶数的组合)>=p(即须要的偶数组);

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

  1. if(odd>=(k-p)&&(odd-(k-p))%2==0&&(even+(odd-(k-p))/2)>=p)

题目难处在于,后面的组合分配,在代码中具体解释。

AC代码例如以下:

  1. #include<iostream>
  2. #include<string>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<cstdlib>
  6. #include<cmath>
  7. #include<algorithm>
  8. #include<map>
  9. #include<queue>
  10. #include<stack>
  11. #include<iomanip>
  12. using namespace std;
  13. #define ll long long
  14. #define M 100005
  15.  
  16. struct H
  17. {
  18. int x,y;//x为值。y为奇偶标记
  19.  
  20. }a[M];
  21.  
  22. int cmp(H q,H w)
  23. {
  24. return q.y<w.y;
  25. }
  26.  
  27. int main()
  28. {
  29. int n,k,p,i,j,t=0;
  30. int even=0,odd=0,cont=0;
  31. cin>>n>>k>>p;
  32. for(i=0;i<n;i++)
  33. {
  34. cin>>a[i].x;
  35.  
  36. if(a[i].x%2==0)
  37. {a[i].y=2;even++;}//标记偶数为2。统计偶数个数
  38. else {a[i].y=1;odd++;}//标记奇数为1,统计奇数个数
  39. }
  40. sort(a,a+n,cmp);//通过对标记值的排序,将数组奇数前置
  41. if(odd>=(k-p)&&(odd-(k-p))%2==0&&(even+(odd-(k-p))/2)>=p)
  42. {
  43. cout<<"YES"<<endl;
  44. if(p==0)//前面一直错就是卡在这。特殊情况的处理
  45. {
  46. for(i=1;i<=k;i++)
  47. {
  48. if(i!=k)
  49. {cout<<"1"<<" "<<a[t++].x<<endl;cont++;}//前面k-1都仅仅输出一个数
  50. else//i=k时把剩余的输出
  51. {
  52. cout<<n-cont<<" ";
  53. for(j=1;j<=n-cont;j++)
  54. cout<<a[t++].x<<" ";
  55. cout<<endl;
  56. }
  57. }
  58. return 0;
  59. }
  60. for(i=1;i<=k;i++)
  61. {
  62. if(i<=k-p)
  63. {cout<<"1"<<" "<<a[t++].x<<endl;cont++;}//首先把奇数全输出
  64. else if(i>k-p&&i<k)
  65. {
  66. if(a[t].y==1)//当还剩余奇数时。因为条件控制,它们肯定是偶数个。所以每两个奇数凑成偶数组
  67. {
  68. {cout<<"2"<<" "<<a[t++].x<<" "<<a[t++].x<<endl;cont+=2;}
  69. }
  70. else
  71. {
  72. {cout<<"1"<<" "<<a[t++].x<<endl;cont++;}
  73. }
  74. }
  75. else//把剩余的输出
  76. {
  77. cout<<n-cont<<" ";
  78. for(j=1;j<=n-cont;j++)
  79. cout<<a[t++].x<<" ";
  80. cout<<endl;
  81. }
  82. }
  83.  
  84. }
  85. else
  86. cout<<"NO"<<endl;
  87. return 0;
  88. }

CF 439C(251C题)Devu and Partitioning of the Array的更多相关文章

  1. CF 439C Devu and Partitioning of the Array

    题目链接: 传送门 Devu and Partitioning of the Array time limit per test:1 second     memory limit per test: ...

  2. Codeforces 439C Devu and Partitioning of the Array(模拟)

    题目链接:Codeforces 439C Devu and Partitioning of the Array 题目大意:给出n个数,要分成k份,每份有若干个数,可是仅仅须要关注该份的和为奇数还是偶数 ...

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

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

  5. codeforces 439C Devu and Partitioning of the Array(烦死人的多情况的模拟)

    题目 //这是一道有n多情况的烦死人的让我错了n遍的模拟题 #include<iostream> #include<algorithm> #include<stdio.h ...

  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. thinkphp5项目--个人博客(八)

    thinkphp5项目--个人博客(八) 项目地址 fry404006308/personalBlog: personalBloghttps://github.com/fry404006308/per ...

  2. 安卓第一课:android studio 的环境搭建与真机运行以及遇到的问题

    AS的下载: https://developer.android.com/studio/index.html AS的安装: android studio, sdk, virtual device都要安 ...

  3. 数据仓库 SSIS

    SSDT 下载 :https://msdn.microsoft.com/en-us/library/mt204009.aspx Codeplex 上的 AdventureWorks 示例数据库此链接将 ...

  4. html页面颜色名称和颜色值转换

    public static string ToHtmlColor(string colorName) { try { if (colorName.StartsWith("#")) ...

  5. PyCharm 2017 Mac 免注册版破解安装说明

    PyCharm 2017 Mac 免注册版破解安装说明 下载完成安装包后,双击打开,将左侧拖拽至右侧应用程序,默认安装. 打开软件,在License server address中填入[http:// ...

  6. ubuntu 同时安装anaconda2和anaconda3

    说明:先根据Ubuntu预装的python2.7来安装Anaconda2,然后将Anaconda3作为其环境安装在envs文件夹下. 重要提示:有一些软件需要py2.7的环境,比如XX-Net, 最好 ...

  7. ubuntu -redis

    ubentu 布置redis,基本操作和CentO感觉相差不多,主要是使用命令有所差异 mark如下: ① download ② tar -zxvf xxx.tar.gz ③ cd redis-xxx ...

  8. python异步IO-asyncio、async和await

    参考链接: asyncio:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00 ...

  9. caioj 1086 动态规划入门(非常规DP10:进攻策略)

    一开始看到题目感觉很难 然后看到题解感觉这题贼简单,我好像想复杂了 就算出每一行最少的资源(完全背包+二分)然后就枚举就好了. #include<cstdio> #include<a ...

  10. 一个HelloWorld版的MySQL数据库管理器的设计与实现(源码)

    2011年,实习期间写了一个简单的数据库管理器. 今天,特意整理了下,分享给大家. 有兴趣的同学,可以下载源码,瞧瞧. 源码只有4个类:LoginGUI,DatabaseGUI,Record,MySQ ...