zoukankan      html  css  js  c++  java
  • CodeForces

    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.

    Examples input
    3
    5 3
    7 18 3 14 1
    5 4
    1 2 3 4 5
    6 2
    1 2 8 4 10 2
    Examples output
    YES
    1 3 5
    NO
    NO

    题意:

    给你你个数, 分成k块, 求是否可以每一块的和都为奇数

    题解:

    对于奇偶的问题, 要首先想到奇偶的交换律, 即
    奇+奇 = 偶
    偶+偶 = 偶
    奇+偶 = 奇
    这样的话, 我们考虑, 在奇数的个数>=k时, 当且仅当奇数的个数%2同k%2相同时, 即说明一定有解

    代码:

    #include <bits/stdc++.h>
    #define LL long long
    const int N = 500000 + 5;
    using namespace std;
    
    LL a[N];
    int main()
    {
        int q;
        scanf("%d", &q);
        while (q--)
        {
            LL n, k;
            scanf("%lld%lld", &n, &k);
    
            LL sum = 0;
            for (int i = 1; i <= n; i++)
            {
                scanf("%lld", &a[i]);
                if (a[i] % 2)
                    sum++;
            }
    
            if (sum < k)
                printf("NO
    ");
            else
            {
                if ((sum - (k + 1)) % 2 == 0)
                    printf("NO
    ");
                else
                {
                    printf("YES
    ");
                    if (k > 1)
                    {
                        int cnt = 0;
                        LL sum = 0;
                        for (int i = 1; i <= n; i++)
                        {
                            sum += a[i];
                            if (sum % 2 == 1)
                            {
                                cnt++;
                                printf("%d ", i);
                                sum = 0;
                            }
                            if (cnt >= k - 1)
                                break;
                        }
                    }
                    printf("%d
    ", n);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    Android UI法宝的设计资源的开发
    Ural 1309 Dispute (递归)
    ZOJ3827 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江I称号 Information Entropy 水的问题
    myeclipse如何恢复已删除的文件和代码
    在C#主线程和子线程将数据传递给对方如何实现
    SSh框架结构(Struts2.1+Hibernate4.0+Spring3.1)
    基于大数据分析的安全管理平台技术研究及应用【摘录】
    ulimit -t 引起的kill血案
    Oracle RAC 环境下的连接管理
    SMTP协议--在cmd下利用命令行发送邮件
  • 原文地址:https://www.cnblogs.com/YY666/p/11338469.html
Copyright © 2011-2022 走看看