• 246C Beauty Pageant


    C. Beauty Pageant
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants.

    All soldiers in the battalion have different beauty that is represented by a positive integer. The value ai represents the beauty of the i-th soldier.

    On each of k days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of k days the beauty of the sent detachment should be unique. In other words, all k beauties of the sent detachments must be distinct numbers.

    Help Payne choose k detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty.

    Input

    The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤  ) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 107) — the beauties of the battalion soldiers.

    It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty.

    Output

    Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer ci (1 ≤ ci ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and ci distinct integersp1, i, p2, i, ..., pci, i — the beauties of the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order.

    Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them.

    Sample test(s)
    input
    3 3 1 2 3
    output
    1 1 1 2 2 3 2
    input
    2 1 7 12
    output
    1 12 

    分析:从头到尾将每个元素逐个考虑,从上一个元素截至得到的vector融入该元素,得到一个新的vector,直到vector个数等于k。vector用来保存,和互不相同的包含各状态信息的结构体。

    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<iostream>
    #include<vector>
    #include<set>
    using namespace std;
    
    typedef struct S {
        int sum;
        bool vis[52];
        int cnt;
    } NODE;
    NODE node[1300];
    int a[52], tot;
    vector<int> b;
    
    int main() {
        int n, k, i, j, m, end = 1;
        scanf("%d%d", &n, &k);
        for (i = 1; i <= n; ++i)
            scanf("%d", &a[i]);
        node[tot].sum = a[1];
        b.push_back(a[1]);
        memset(node[tot].vis, 0, sizeof (node[tot].vis));
        node[tot].vis[1] = 1;
        node[tot].cnt = 1;
        printf("%d", node[tot].cnt);
        for (j = 1; j <= 1; ++j)
            if (node[tot].vis[j])
                printf(" %d", a[j]);
        printf("\n");
        ++tot;
        if (tot == k)
            return 0;
        for (i = 2; i <= n; ++i) {
            node[tot].sum = a[i];
            if (count(b.begin(), b.end(), node[tot].sum))
                goto L;
            b.push_back(a[i]);
            memset(node[tot].vis, 0, sizeof (node[tot].vis));
            node[tot].vis[i] = 1;
            node[tot].cnt = 1;
            printf("%d", node[tot].cnt);
            for (j = 1; j <= i; ++j)
                if (node[tot].vis[j])
                    printf(" %d", a[j]);
            printf("\n");
            ++tot;
            if (tot == k)
                return 0;
            L:
            for (m = 0; m < end; ++m) {
                node[tot].sum = node[m].sum + a[i];
                if (count(b.begin(), b.end(), node[tot].sum))
                    continue;
                b.push_back(node[tot].sum);
                for (j = 1; j < i; ++j)
                    node[tot].vis[j] = node[m].vis[j];
                node[tot].vis[i] = 1;
                node[tot].cnt = node[m].cnt + 1;
                printf("%d", node[tot].cnt);
                for (j = 1; j <= i; ++j)
                    if (node[tot].vis[j])
                        printf(" %d", a[j]);
                printf("\n");
                ++tot;
                if (tot == k)
                    return 0;
            }
            end=tot;
        }
        return 0;
    }
  • 相关阅读:
    利用FormData对象实现AJAX文件上传功能及后端实现
    $.ajax not function(已 解决:jQuery库冲突解决办法)
    什么是docx,pptx等的正确MIME类型?
    session_end
    cookies.Expires (小技巧)
    如何生成静态页面的五种方案(转)
    Request.UserHostAddress和Request.Url.Host(小技巧)
    单点登录的一些实现
    框架页的session和cookies
    在IHttpHandler使用session
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2782159.html
Copyright © 2020-2023  润新知