B. Odd Sum Segments(基础数论)

☆樱花仙子☆ 提交于 2019-11-27 14:56:20

题目地址:http://codeforces.com/contest/1196/problem/B

You are given an array aa consisting of nn integers a1,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 5 into 3subsegments (not necessarily with odd sums): [1,2,3,4,5]  is the initial array, then all possible ways to divide it into 3 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⋅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⋅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≤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).

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]Note that rkrk is always nn but you should print it anyway.

Example

input

Copy

3
5 3
7 18 3 14 1
5 4
1 2 3 4 5
6 2
1 2 8 4 10 2

output

Copy

YES
1 3 5
NO
NO

题目大意:给你n个数,分成k个序列,判断是否每组的和是奇数,不是输出NO,是的话,分别输出分好的k的最右边坐标(答案不唯一)

思路概括:

对于奇偶的问题, 要首先想到奇偶的交换律, 即
奇+奇 = 偶
偶+偶 = 偶
奇+偶 = 奇

奇数个奇数相加==奇数,奇数个奇数+偶数==奇数,奇数+奇数==偶数

所以,在数列中我们乐意忽略偶数的存在,首先,统计奇数的个数,要分成m个组,假设每一组1个奇数,则至少需要m个奇数,如果奇数个数比k还小,是无法分出来的,直接输出NO,根据贪心的思想,从前往后,没for到一个奇数,就输出他的位置,知道还剩最后一组,看一下剩余的奇数个数是否是奇数个

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define lt k<<1
#define rt k<<1|1
#define lowbit(x) x&(-x)
#define lson l,mid,lt
#define rson mid+1,r,rt
using namespace std;
typedef long long ll;
typedef long double ld;
#define ios ios::sync_with_stdio(false);cin.tie(nullptr);
#define mem(a, b) memset(a, b, sizeof(a))
//#define int ll
const double pi = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 2e5 + 50;
int a[maxn];
int main()
{
    int q;
    cin >> q;
    while(q--)
    {
        int cnt = 0;
        int n, k;
        cin >> n >> k;
        for(int i=1;i<=n;i++)
        {
            cin >> a[i];
            if(a[i]%2) cnt++;
        }
        if(cnt % 2 == k %2 && cnt >= k)
        {
            cout << "YES" << endl;
            k--;
            for(int i=1;i<=n && k > 0;i++)
            {
                if(a[i] % 2)
                {
                    cout << i << ' ';
                    k--;
                }
            }
            cout << n << endl;
        }
        else cout << "NO" << endl;
    }
    return 0;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!