B. Odd Sum Segments CF(分割数组)
题目地址
http://codeforces.com/contest/1196/problem/B
3 seconds
256 megabytes
standard input
standard output
You are given an array aa consisting of nn integers a1,a2,…,ana1,a2,…,an. You want to split it into exactly kk non-empty non-intersecting subsegments such that each subsegment has odd sum (i. e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the elements of a given array. Each of the nn elements of the array aa must belong to exactly one of the kk subsegments.
Let's see some examples of dividing the array of length 55 into 33 subsegments (not necessarily with odd sums): [1,2,3,4,5][1,2,3,4,5] is the initial array, then all possible ways to divide it into 33 non-empty non-intersecting subsegments are described below:
- [1],[2],[3,4,5][1],[2],[3,4,5];
- [1],[2,3],[4,5][1],[2,3],[4,5];
- [1],[2,3,4],[5][1],[2,3,4],[5];
- [1,2],[3],[4,5][1,2],[3],[4,5];
- [1,2],[3,4],[5][1,2],[3,4],[5];
- [1,2,3],[4],[5][1,2,3],[4],[5].
Of course, it can be impossible to divide the initial array into exactly kk subsegments in such a way that each of them will have odd sum of elements. In this case print "NO". Otherwise, print "YES" and any possible division of the array. See the output format for the detailed explanation.
You have to answer qq independent queries.
The first line contains one integer qq (1≤q≤2⋅1051≤q≤2⋅105) — the number of queries. Then qq queries follow.
The first line of the query contains two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the number of elements in the array and the number of subsegments, respectively.
The second line of the query contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is the ii-th element of aa.
It is guaranteed that the sum of nn over all queries does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).
For each query, print the answer to it. If it is impossible to divide the initial array into exactly kk subsegments in such a way that each of them will have odd sum of elements, print "NO" in the first line. Otherwise, print "YES" in the first line and any possible division of the array in the second line. The division can be represented as kk integers r1r1, r2r2, ..., rkrk such that 1≤r1<r2<⋯<rk=n1≤r1<r2<⋯<rk=n, where rjrj is the right border of the jj-th segment (the index of the last element that belongs to the jj-th segment), so the array is divided into subsegments [1;r1],[r1+1;r2],[r2+1,r3],…,[rk−1+1,n][1;r1],[r1+1;r2],[r2+1,r3],…,[rk−1+1,n]. Note that rkrk is always nn but you should print it anyway.
- 3
- 5 3
- 7 18 3 14 1
- 5 4
- 1 2 3 4 5
- 6 2
- 1 2 8 4 10 2
- YES
- 1 3 5
- NO
- NO
- 题意: 给定一个n,m。下一行有n个数。问是否可以分割成m组,使每组的和为奇数。如果可以,打印YES,并且输出分割出来的每组的最右边坐标(可能有多种,打印一组即可);否则输出“NO”;
解析: 首先明确一点:偶数+奇数==奇数 奇数+奇数=偶数;
所以呢,在给定的一组数中可以忽略偶数的存在,因为偶数与奇数的运算并不能改变结果的奇偶性。首先统计奇数的个数ob。要分成m组,假设每一组1个奇数,则至少需要m个奇数,因为偶数的存在改变不了结局。
如果奇数个数ob比m还少,肯定分不出来m组。所以要ob>=m; 根据贪心的思想,从前往后分割。在前面,只要for到一个奇数,就算分割到一组,直到还剩最后一组。需要看最后一组奇数个数,ob-(k-1)即为剩余奇数个数
如果是偶数个,肯定不行因为加起来是偶数,输出“NO”;奇数个的话,输出YES,打印最后一位的坐标,就行了。
- #include<iostream>
- #include<vector>
- #include<cstring>
- using namespace std;
- const int maxn=;
- typedef long long ll;
- ll a[maxn];
- int main()
- {
- ll t;
- cin>>t;
- while(t--)
- {
- ll n,m;
- cin>>n>>m;
- ll od=;
- for(int i=;i<n;i++)
- {
- cin>>a[i];
- if(a[i]%!=)
- od++; //统计奇数个数
- }
- if(od>=m&&(od-(m-))%!=)
- {
- cout<<"YES"<<endl;
- ll k=;
- for(int i=;i<n;i++)
- {
- if(a[i]%!=)
- {
- k++; //当前所分的组数
- if(k==m)
- {
- cout<<n<<endl;break;
- }
- else
- {
- cout<<i+<<" ";
- }
- }
- }
- // cout<<endl;
- }
- else
- cout<<"NO"<<endl;
- }
- return ;
- }
B. Odd Sum Segments CF(分割数组)的更多相关文章
- Codeforces Round #575 (Div. 3) B. Odd Sum Segments (构造,数学)
B. Odd Sum Segments time limit per test3 seconds memory limit per test256 megabytes inputstandard in ...
- lintcode 容易题:Partition Array by Odd and Even 奇偶分割数组
题目: 奇偶分割数组 分割一个整数数组,使得奇数在前偶数在后. 样例 给定 [1, 2, 3, 4],返回 [1, 3, 2, 4]. 挑战 在原数组中完成,不使用额外空间. 解题: 一次快速排序就可 ...
- Codeforces Round #575 (Div. 3) B. Odd Sum Segments 、C Robot Breakout
传送门 B题题意: 给你n个数,让你把这n个数分成k个段(不能随意调动元素位置).你需要保证这k个段里面所有元素加起来的和是一个奇数.问可不可以这样划分成功.如果可以打印YES,之后打印出来是从哪里开 ...
- LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$
Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...
- lintcode373 奇偶分割数组
奇偶分割数组 分割一个整数数组,使得奇数在前偶数在后. 您在真实的面试中是否遇到过这个题? Yes 样例 给定 [1, 2, 3, 4],返回 [1, 3, 2, 4]. 我的方法:设定两个数组,分别 ...
- LeetCode 410——分割数组的最大值
1. 题目 2. 解答 此题目为 今日头条 2018 AI Camp 5 月 26 日在线笔试编程题第二道--最小分割分数. class Solution { public: // 若分割数组的最大值 ...
- Leetcode 410.分割数组的最大值
分割数组的最大值 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小. 注意:数组长度 n 满足以下条件: 1 ≤ n ...
- leetcode 410. 分割数组的最大值(二分法)
1. 题目描述 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小. 注意: 数组长度 n 满足以下条件: 1 ≤ n ...
- Odd sum (对本菜鸡来说是个极坑题)
https://codeforces.com/problemset/problem/797/B time limit per test 1 second memory limit per test 2 ...
随机推荐
- 吴裕雄--天生自然java开发常用类库学习笔记:IdentityHashMap类
import java.util.IdentityHashMap ; import java.util.HashMap ; import java.util.Set ; import java.uti ...
- created a ThreadLocal with key of type [oracle.jdbc.driver.AutoKeyInfo$1]
环境: spring4.3, mybatis3.5.2, ojdbc8_8c(oracle 18c jdbc) 调试状态下退出时,提示: 严重 [main] org.apache.catalina.l ...
- Docker自建本地仓库
1.安装docker yum install docker -y 之后根据需求把指定docker安装目录,或者新建一个磁盘使用docker-storage-setup来使用 2.设置第三方docker ...
- 《学习R》笔记:科学计算器、检查变量和工作区、向量、矩阵和数组、列表和数据框
一.第二章 科学计算器 要检查两个数字是否一样,要使用 all.equal() ,不要使用 == ,== 符号仅用于比较两个整型数是否存在相同 . > all.equal(sqrt(2)^2,2 ...
- 四十七、在SAP中,把功能区块整合成一个函数,通过调用函数的办法使代码简洁明了
一.我们查看上一次的代码,非常之凌乱,大体可以分为以下这几个区块 二.我们把最后的2个部分,用函数的方式来写,写法如下: 三.执行程序,和之前一样 四.输出结果
- mysql多表连接查询
新建两张表: 表1:student 截图如下: 表2:course 截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际开发中我们不会这样建表,实际开发中这两个表会有自己不同的主键.) ...
- RabbitMQ消息队列帮助类
调用 //消息队列发消息 MqConfigInfo config = new MqConfigInfo(); config.MQExChange = "DrawingOutput" ...
- JNI操作二维数组
之前的文章讲解了有关JNI使用方法,这篇文章进阶一点,介绍下JNI操作二维数组的方法.有了之前文章的操作JNI的方法,这里直接上代码了. Java代码部分 package com.testjni; p ...
- Android Studio真机调试安装以后打开闪退,打包APK再安装正常打开没有问题
一直真机调试都没有问题, 但是有一次开始,真机调试正常安装没有问题,但是一打开就崩溃了一眨眼间就像被光闪了一下的那种. oppoR11调试会这样,但是用魅族试过没有问题, 报错出现过Android S ...
- python中的__code__
简单总结几个常用的__code__的用法: (1)func.__code__.co_argcount:返回函数的参数个数,这里的参数个数不包含*args与**kwargs,具体来讲就是*args前的参 ...