B. Odd Sum Segments

传送门

B. Odd Sum Segments

time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

Input
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).

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

Example
Input
3
5 3
7 18 3 14 1
5 4
1 2 3 4 5
6 2
1 2 8 4 10 2
Output
YES
1 3 5
NO
NO

题意:将长度为n的数组a划分成k个非空组,确保所有的组内之和都为奇数。可以划分则第一行输出YES,第二行输出奇数对应的坐标。否则输出NO。

思路:首先计算数组中奇数个数,如果奇数个数等于k,则每组一个奇数,满足需求。如果奇数个数比k大奇数个,则这些奇数一定会导致一个组的和变成偶数,不满足。如果奇数个数比k大偶数个,则满足要求。根据贪心的思想,从前往后,没for到一个奇数,就输出他的位置,知道还剩最后一组,看一下剩余的奇数个数是否是奇数个

AC代码

#include <bits/stdc++.h>
using namespace std;
vector<int>ans;
int main()
{
    int t,n,a,k,sum1,sum2;
    cin>>t;
    while(t--)
    {
        ans.clear();
        sum1=sum2=0;
        cin>>n>>k;
        for(int i=1;i<=n;++i)
        {
            cin>>a;
            if(a%2)
                sum1++,ans.push_back(i);
            else
                sum2++;
        }
        if(sum1<k)
            cout<<"NO"<<endl;
        else if((k-sum1)%2)
            cout<<"NO"<<endl;
        else
        {
            cout<<"YES"<<endl;
            for(int i=0;i<k-1;++i)
                cout<<ans[i]<<" ";
            cout<<n<<endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

稚皓君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值