CF 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
Description
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 ≤ 10^5; 0 ≤ p ≤ k). The next line will contain n space-separated distinct integers representing the content of array a: a1, a2, ..., an (1 ≤ ai ≤ 10^9).
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 Input
5 5 3
2 6 10 5 9
5 5 3
7 14 2 9 5
5 3 1
1 2 3 7 5
Sample Output
YES
1 9
1 5
1 10
1 6
1 2
NO
YES
3 5 1 3
1 7
1 2
解题思路:
题目大意:给n个数字,问能够将这n个数分成p堆,每堆和为偶数,k-p堆,每堆和为奇数
简单分堆,稍微注意一下细节处理。
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int n,k,p;
while (~scanf("%d%d%d",&n,&k,&p))
{
int tmp;
vector<int>itv1,itv2;
for (int i = 0; i < n; i++)
{
scanf("%d",&tmp);
if (tmp & 1)
{
itv1.push_back(tmp);
}
else
{
itv2.push_back(tmp);
}
}
int len1 = itv1.size();
int len2 = itv2.size();
if (len1 < (k - p) || ((len1 - (k - p))&1) || ((len1 - (k - p))/2 + len2 < p))
{
printf("NO\n");
}
else
{
int x = k - p;
printf("YES\n");
for (int i = 0;i < x - 1;i++)
{
printf("1 %d\n",itv1.back());
itv1.pop_back();
}
for (int i = 0;i < p - 1;i++)
{
if (!itv2.empty())
{
printf("1 %d\n",itv2.back());
itv2.pop_back();
}
else
{
printf("2");
for (int j = 0;j < 2;j++)
{
printf(" %d",itv1.back());
itv1.pop_back();
}
printf("\n");
}
}
if (x && p)
{
printf("1 %d\n",itv1.back());
itv1.pop_back();
}
printf("%d",itv1.size()+itv2.size());
while (!itv1.empty())
{
printf(" %d",itv1.back());
itv1.pop_back();
}
while (!itv2.empty())
{
printf(" %d",itv2.back());
itv2.pop_back();
}
printf("\n");
}
}
return 0;
}
CF 439C Devu and Partitioning of the Array的更多相关文章
- 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 439C Devu and Partitioning of the Array(烦死人的多情况的模拟)
题目 //这是一道有n多情况的烦死人的让我错了n遍的模拟题 #include<iostream> #include<algorithm> #include<stdio.h ...
- 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 ...
- 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 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]的大小 ...
随机推荐
- 完美演绎DevExpress XtraPrinting Library 的打印功能
完美演绎DevExpress XtraPrinting Library 的打印功能 2010-05-14 17:40:49| 分类: 默认分类|字号 订阅 设计报告不仅费时间,而且还乏味!但 ...
- JavaEE 获取路径全攻略
本篇博客是 JavaWeb 应用服务器端在不同环境下获取文件路径的全面总结. 获取文件路径后主要应用的场景,读取 JavaWeb 自定义配置文件.在特定路径下生成各种类型的文件提供下载...... 想 ...
- Android热修复实践应用--AndFix
一直关注App的热修复的技术发展,之前做的应用也没用使用到什么热修复开源框架.在App的热修复框架没有流行之前,做的应用上线后发现一个小小的Bug,就要马上发一个新的版本.我亲身经历过一周发两个版本, ...
- HTML5+JS 《五子飞》游戏实现(七)游戏试玩
前面第一至第六章我们已经把<五子飞>游戏的基本工作都已经讲得差不多了,这一章主要是把所有的代码分享给大家,然后小伙伴们也可以玩一玩. 至于人机对战的我们放到后面讲进行分析. 试玩地址:ht ...
- Linux vmstat命令实战详解
vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...
- SQLite安装、编译与应用
什么是 SQLite SQLite是一款轻量级的.基于文件的嵌入式数据库,实现自包容.零配置.支持事务的SQL数据库引擎.与其他数据库管理系统不同,SQLite 的安装和运行非常简单,在大多数情况下, ...
- [BZOJ1264][AHOI2006]Match(DP+树状数组)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1264 分析: 考虑做一般的LCS的时候,更新结果的条件是a[i]==b[j]时候 于是 ...
- 关于提高python程序执行效率的思路和想法
相比编译型语言(C系列)python胜在简介的语法和优雅的动态编程体验,但是在执行效率上,python有解释性语言先天的劣势——执行效率较低,为了让编写出的程序获得更快的执行效率,开启此文章. pyt ...
- 揭秘PHP匿名函数
揭秘PHP匿名函数 定义:匿名函数就是没有名字的函数. 有2种形式的匿名函数: 形式1:将一个匿名函数"赋值"给一个变量--此时该变量就代表该匿名函数了! 形式2: 是直接将一个匿 ...
- oracle如何获取每个月的最后一天
SELECT LAST_DAY(DATE'2016-09-23') FROM DUAL;