zoukankan      html  css  js  c++  java
  • Codeforces Round #575 (Div. 3) B

    Codeforces Round #575 (Div. 3)

    B - Odd Sum Segments

    You are given an array a consisting of n integers a1,a2,…,an. You want to split it into exactly k 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 n elements of the array a must belong to exactly one of the k subsegments.

    Let's see some examples of dividing the array of length 5 into 3 subsegments (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].

    Of course, it can be impossible to divide the initial array into exactly k 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 q independent queries.

    Input

    The first line contains one integer q (1≤q≤2⋅105) — the number of queries. Then q queries follow.

    The first line of the query contains two integers n and k (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 n integers a1,a2,…,an (1≤ai≤109), where ai is the i-th element of a.

    It is guaranteed that the sum of n over all queries does not exceed 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 k 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 k integers r1, r2, ..., rk such that 1≤r1<r2<⋯<rk=n, where rj is the right border of the j-th segment (the index of the last element that belongs to the j-th segment), so the array is divided into subsegments [1;r1],[r1+1;r2],[r2+1,r3],…,[rk−1+1,n]. Note that rk is always n 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个数,问你能不能分成k堆奇数堆。不能输出 "NO";

    能输出 "YES",以及这K堆奇数堆的最后一个元素位置,显然最后一堆最后一个位置为 n 。

    思路:我们可以先找出原数组中奇数的个数为 cnt,

    显然,当 cnt<k 或者 cnt%2 != k%2 时,答案为 "NO" ,可以尝试着自己推一下奇偶性。

    否则为 "YES" ,然后只需输入前k-1个奇数位置和最后一个n就等价于分成k堆奇数堆了。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<vector>
     7 #include<map>
     8 #include<set>
     9 #include<vector>
    10 #include<queue>
    11 using namespace std;
    12 #define ll long long
    13 const int mod=1e9+7;
    14 const int inf=1e9+7;
    15    
    16 const int maxn=2e5+10;
    17  
    18 int main()
    19 {
    20     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    21      
    22     int T;
    23     cin>>T;
    24     
    25     int n,k;
    26     ll int num;
    27     while(cin>>n>>k)
    28     {
    29         vector<int>place;
    30         
    31         for(int i=0;i<n;i++)
    32         {
    33             cin>>num;
    34             
    35             if(num&1)
    36                 place.push_back(i+1);
    37         }
    38         
    39         if(place.size()<k||(place.size()%2!=k%2))
    40         {
    41             cout<<"NO"<<endl;
    42             continue;
    43         }
    44         
    45         cout<<"YES"<<endl;
    46         
    47         for(int i=0;i<k-1;i++)
    48             cout<<place[i]<<" ";
    49         cout<<n<<endl;
    50         
    51      } 
    52 
    53     return 0;
    54  }
    大佬见笑,,
  • 相关阅读:
    Python 的 Pandas 对矩阵的行进行求和
    python 统计字符串中指定字符出现次数的方法
    Python 2 代码转 Python 3的一些转化
    Python 的文件保存路径
    Pycharm 个人认为舒服漂亮又好用的主题风格
    【RS】Collaborative Memory Network for Recommendation Systems
    MATLAB 条形图添加多个图例
    MATLAB 画柱状图(/直方图)修改横坐标名称并使其横着显示
    Nexus3.X 将Maven项目上传至nexus私服
    nexus maven 使用案例
  • 原文地址:https://www.cnblogs.com/xwl3109377858/p/11271808.html
Copyright © 2011-2022 走看看