CF 439C(251C题)Devu and Partitioning of the Array
1 second
256 megabytes
standard input
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.
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).
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.
- 5 5 3
- 2 6 10 5 9
- YES
- 1 9
- 1 5
- 1 10
- 1 6
- 1 2
- 5 5 3
- 7 14 2 9 5
- NO
- 5 3 1
- 1 2 3 7 5
- 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;
- }
CF 439C(251C题)Devu and Partitioning of the Array的更多相关文章
- CF 439C Devu and Partitioning of the Array
题目链接: 传送门 Devu and Partitioning of the Array time limit per test:1 second memory limit per test: ...
- Codeforces 439C Devu and Partitioning of the Array(模拟)
题目链接:Codeforces 439C Devu and Partitioning of the Array 题目大意:给出n个数,要分成k份,每份有若干个数,可是仅仅须要关注该份的和为奇数还是偶数 ...
- 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 ...
- 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 ...
- codeforces 439C Devu and Partitioning of the Array(烦死人的多情况的模拟)
题目 //这是一道有n多情况的烦死人的让我错了n遍的模拟题 #include<iostream> #include<algorithm> #include<stdio.h ...
- Codeforces Round #251 (Div. 2) C. Devu and Partitioning of the Array
注意p的边界情况,p为0,或者 p为k 奇数+偶数 = 奇数 奇数+奇数 = 偶数 #include <iostream> #include <vector> #include ...
- codeforces 439D Devu and Partitioning of the Array(有深度的模拟)
题目 //参考了网上的代码 注意答案可能超过32位 //要达成目标,就是要所有数列a的都比数列b的要小或者等于 //然后,要使最小的要和最大的一样大,就要移动(大-小)步, //要使较小的要和较大的一 ...
- codeforces C. Devu and Partitioning of the Array
题意:给你n个数,然后分成k部分,每一个部分的和为偶数的有p个,奇数的有k-p个,如果可以划分,输出其中的一种,不可以输出NO; 思路:先输出k-p-1个奇数,再输出p-1个偶数,剩余的在进行构造. ...
- 【Henu ACM Round#20 D】 Devu and Partitioning of the Array
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 一开始所有的数字单独成一个集合. 然后用v[0]和v[1]记录集合的和为偶数和奇数的集合它们的根节点(并查集 然后先让v[0]的大小 ...
随机推荐
- react --- React中state和props分别是什么?
props React的核心思想就是组件化思想,页面会被切分成一些独立的.可复用的组件. 组件从概念上看就是一个函数,可以接受一个参数作为输入值,这个参数就是props,所以可以把props理解为从外 ...
- [C++] upper_bound和lower_bound
upper_bound 源码 template <class ForwardIterator, class T> ForwardIterator upper_bound (ForwardI ...
- SQL SERVER中求上月、本月和下月的第一天和最后一天
1.上月的第一天 SELECT CONVERT(CHAR(10),DATEADD(month,-1,DATEADD(dd,-DAY(GETDATE())+1,GETDATE())),111) 2.上月 ...
- java高效判断素数
java高效判断素数 package solution; public class Prime { // 偶数可以由有两个素数相加得到, 一个偶数可能有多个这样的两个素数, 请寻找到 这样两个素数,让 ...
- c# window服务-初学习
window服务-初学习 一.工具: VS2015+NET Framework4.5. 二.操作: 1.新建windows服务的项目: 2.修改windows服务相关内容: 3.预览windows服务 ...
- WHU 1548 Home 2-SAT
---恢复内容开始--- 题意: N个人想回家在至少一个时刻.至多两个时刻.并且,他们每个人都能独自回家. 定义:ai表示第i个人回家的时间, xij = abs(ai - aj) (i != j). ...
- web——前后端通信原理
前端向后台传输数据: 传输方法:post get 区别: (1)get:用于从服务器获取数据,将参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看 ...
- 二叉树、B树、B+树、B*树、LSM树
HBase 对于数据产品,底层存储架构直接决定了数据库的特性和使用场景.RDBMS(关系型数据库)使用 B树 及 B+树 作为数据存储结构. HBase 使用 LSM树. . 二叉树 ...
- dynamic_cast与能力查询
在C++里面,dynamic_cast 通常用于横向转换,而不是向上或者向下的转换. 这个常常用于检查某个实例,是否实现了某个接口类,那么就把这个实例,用dynamic_cast来转换成这个接口类的实 ...
- 基于ArcGIS Flex API实现动态标绘(1.2)
动态标绘API 1.2,相较前一版本号(点击进入),该版本号新增对基本标绘符号的支持,包含: 单点.多点.折线.手绘线.多边形.手绘多边形.矩形,并提供对应的编辑功能. 例如以下图所看到的,对多点的编 ...